PHP strpos Function

PHP Tutorial > String Functions > strpos Function

The strpos function in PHP is used to find the position of the first match in a string. The syntax of the strpos function is:

strpos ('string', 'match_pattern', [offset])

The optional offset parameter tells the function to start looking for the match after the offset-th character in the string. The value returned by the function still indicates the position of the first match relative to the entire string.

Let's take a look at the examples below:

Example 1

print strpos ('Hurrah Hurrah','Hur');



In this example, the match occurs at the very beginning of the string, hence the function returns 0.

Example 2

print strpos ('Hurrah Hurrah', 'Hur', 3);



The offset value of 3 means that we start looking for the match in position 3. From there, the first match occurs at position 7.

Next: PHP strstr Function

Copyright © 2023  All Rights Reserved.  Privacy Policy   About   Contact