PHP stripos() Function

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

It has the following syntax:

PHP

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

Here is an example of using stripos():

PHP

// Output — 2
echo stripos("I love Apples, I love Bananas too!","love");

Additional Information — Please note that stripos() is case-insensitive. It is also binary-safe.

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.

PHP Version and Changelog — The stripos() function is available in PHP 5, PHP 7. The support for negative offsets has been added in PHP version 7.1.0.

Relevant Functions — Other related PHP functions that you should know about are: mb_stripos which finds position of first occurrence of a string within another, case insensitive, strpos() which finds the position of the first occurrence of a substring in a string, 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, stristr() which is case-insensitive strstr(), substr() which returns part of a string and str_ireplace() which is case-insensitive version of str_replace().

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

Parameters

haystack

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

needle

The needle parameter is used to specify the string that we want to find. Please note that the needle may be a string of one or more characters. This is also a required parameter.

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

offset

The offset parameter is used to specify the number of characters in the main string after which the 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 also an optional parameter.

Working Examples

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

PHP

$findme    = 'a';
$mystring1 = 'ABC';
$pos1 = stripos($mystring1, $findme);

// Output — The string 'a' was not found in 'ABC'
if ($pos1 == false) {
    echo "The string '$findme' was not found in '$mystring1'";
}

// Output — We found 'a' in 'ABC' at position 0.
if ($pos1 !== false) {
    echo "We found '$findme' in '$mystring2' at position $pos2";
}

In the first case, the index value 0 evaluates to false, that’s why the ‘not found’ statement got printed even though stripos() performed case-insensitive search.

Additional Tips

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

  1. I found myself needing to find the first position of multiple needles in one haystack. So I wrote this little function:

    PHP

    function multineedle_stripos($haystack, $needles, $offset=0) {
        foreach($needles as $needle) {
            $found[$needle] = stripos($haystack, $needle, $offset);
        }
        return $found;
    }
    $haystack = "The quick brown fox jumps over the lazy dog.";
    $needle = array("fox", "dog", ".", "duck");
    
    var_dump(multineedle_stripos($haystack, $needle));
    /* Output:
    array(3) {
     ["fox"]=>
     int(16)
     ["dog"]=>
     int(40)
     ["."]=>
     int(43)
     ["duck"]=>
     bool(false)
    }  */
    

    Suggested by - emperorshishire at gmail dot com

  2. It is very bad practice to use the same function name as an existing PHP function but have a different output format. Someone maintaining the code in the future is likely to be very confused by this. It will also be hard to eradicate from a codebase because the naming is identical so each use of stripos() would have to be analyzed to see how it is expecting the output format (bool or number/bool).

    Suggested by - spam at kleppinger dot com

Further Reading

  1. You can read more about the PHP stripos() 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