strrpos()
strrpos() — Find the position of the last occurrence of a substring in a string.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, 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
strrpos(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:
$mystring = 'Elephpant'; $pos = strrpos($mystring, "b"); if ($pos === false) { // note: three equal signs // not found... }
Changelog
| Version | Change |
|---|---|
| 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(), strripos(), strrchr(), substr()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page strrpos() (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.