


How to use strpos in php to get the first occurrence of a string
The method of strpos in php to get the position where a string first appears: first create a PHP sample file; then define a string; finally get the string through "strpos($a,"php");" The location where it first appears is sufficient.
#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
strpos() function is used to find the position where a string appears for the first time in another string. If the specified string is found in the string, it returns the position found starting from 0. If If not, the logical value false is returned.
Specific usage of strpos() function in php
strpos(搜索目标字符串,搜索字符串[,搜索字符串中的起始位置])
It determines whether the target string has a string that needs to be searched, and if it exists, returns the position.
Otherwise return false. If options are specified, the starting position can be changed while searching.
Let’s look at a simple example
Search for “php” in “php Chinese website”
<?php echo strpos("php中文网","php"); ?>
The output results are displayed is 0. If the string php is changed to Chinese website, the output result is 3.
In PHP, 0 may be treated as false depending on how it is written, so you need to pay attention.
Must be used in conditional expressions! == or ===.
<?php $a = "php中文网"; if(strpos($a,"php")){ echo "php中文网中有这个字符"; } echo strpos($a,"php"); ?>
The above code searches whether the text php is included in "php Chinese website". Since php is included, a character named php should be displayed, but it will not actually be displayed. This is because the text character The string "php" starts from the first part of $a, so the value returned by strpos is "0".
By setting false in the conditional expression! == strpos($a, "php") will do the trick.
This article ends here. For more exciting content, you can pay attention to the relevant tutorial columns of PHP Chinese! ! !
The above is the detailed content of How to use strpos in php to get the first occurrence of a string. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...
