PHP strpos() Function

Usage — The PHP strpos() function is used to find the numeric position of the first occurrence of a substring in a string.

It has the following syntax:

PHP

mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

Here is an example of using strpos():

PHP

// Output — 26
echo strpos("I love apples, Ross loves Apples too!","Apples");

Return Value — This function returns the position where the needle exists relative to the beginning of the haystack string (independent of offset). You should also note that string positions start at 0, and not 1. It returns FALSE if the needle was not found.

Additional Information — Please keep in mind that the strpos() function is case-sensitive. It is also binary-safe.

PHP Version and Changelog — The strpos() function is available in PHP 4, PHP 5, PHP 7. PHP version 7.1.0 also includes support for negative values for the offset parameter.

Relevant Functions — Other related PHP functions that you should know about are: stripos() which finds the position of the first occurrence of a case, strrpos() which finds the position of the last occurrence of a substring in a string, strripos() which finds the position of the last occurrence of a case, strstr() which finds the first occurrence of a string, strpbrk() which searches a string for any of a set of characters, substr() which returns part of a string and preg_match which performs a regular expression match.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

haystack

The haystack parameter is used to specify the string we want to search in. This is a required parameter.

needle

The needle parameter is used to specify the substring we are looking for. If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. This is a required parameter.

offset

The offset parameter is used to specify the number of characters counted from the beginning of the string after which search will start . If the offset is negative, the search will start this number of characters counted from the end of the string. This is an optional parameter.

Working Examples

Here are some examples of using the strpos() function:

In the following example, note the use of === operator. Simply using == would not work as expected because the position of ‘a’ was the 0th (first) character.

PHP

$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
 

In the following example, note the use of !== operator. Simply using == would not work as expected because the position of ‘a’ was the 0th (first) character. The statement (0 != false) evaluates to false.

PHP

$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);
if ($pos !== false) {
     echo "The string '$findme' was found in the string '$mystring'";
         echo " and exists at position $pos";
} else {
     echo "The string '$findme' was not found in the string '$mystring'";
}
 

Finally, observe how the use of an offset changes the final output of the strpos() function from 0 to 7.

PHP

$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. As strpos may return either FALSE (substring absent) or 0 (substring at start of string), strict versus loose equivalency operators must be used very carefully.

    To know that a substring is absent, you must use: === FALSE.

    To know that a substring is present (in any position including 0), you can use either of: !== FALSE (recommended) or > -1.

    To know that a substring is at the start of the string, you must use: === 0.

    To know that a substring is in any position other than the start, you can use any of: > 0 (recommended), != 0 (note: but not !== 0 which also equates to FALSE), != FALSE (not recommended as highly confusing).

    Also note that you cannot compare a value of "" to the returned value of strpos(). With a loose equivalence operator (== or !=) it will return results which don’t distinguish between the substring’s presence versus position. With a strict equivalence operator (=== or !==) it will always return FALSE.

    Suggested by - Suggested re-write for pink WARNING box

Further Reading

  1. You can read more about the PHP strpos() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top