strripos()
strripos() — Find the position of the last occurrence of a case-insensitive substring in a string.
Built-in function of PHP 8.5. The manual lists it as available in PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
strripos(string $haystack, string $needle, int $offset = 0): int|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $haystack | string | The string to search in. |
| $needle | string | The string to search for. |
| $offset | int | If zero or positive, the search is performed left to right skipping the first offset bytes of the haystack. |
Return value
Returns the position where the needle exists relative to the beginning of the haystack string (independent of search direction or offset). Note: String positions start at 0, and not 1. Note: String positions start at 0, and not 1. String positions start at 0, and not 1. Returns false if the needle was not found. This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
Example
A runnable snippet:
$haystack = 'ababcd'; $needle = 'aB'; $pos = strripos($haystack, $needle); if ($pos === false) { echo "Sorry, we did not find `$needle` in `$haystack`"; } else { echo "Congratulations!\n"; echo "We found the last `$needle` in `$haystack` at position `$pos`"; }
Output:
Congratulations! We found the last `aB` in `ababcd` at position `2`
Changelog
| Version | Change |
|---|---|
| 8.2.0 | Case folding no longer depends on the locale set with setlocale(). Only ASCII case folding will be done. Non-ASCII bytes will be compared by their byte value. |
| 8.0.0 | needle now accepts an empty string. |
| 8.0.0 | Passing an int as needle is no longer supported. |
| 7.3.0 | Passing an int as needle has been deprecated. |
See also
strpos(), stripos(), strrpos(), strrchr(), stristr(), substr()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page strripos() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.