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');

Result:

0

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);

Result:

7

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

Link to this page: If you find this page useful, we encourage you to link to this page. Simply copy and paste the code below to your website, blog, or profile.




More 1Keydata Tutorials



PHP addslashes
PHP echo
PHP explode
PHP htmlentities
PHP htmlspecialchars
PHP implode
PHP md5
PHP number_format
PHP print
PHP str_replace
PHP strlen
PHP strpos
PHP strstr
PHP substr
PHP trim

PHP Syntax

PHP Sitemap

PHP Resources