mb_strrpos()
mb_strrpos() — Find position of last occurrence of a string in a string.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.6, 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.
What it does
Performs a multibyte safe strrpos() operation based on the number of characters. needle position is counted from the beginning of haystack. First character's position is 0. Second character position is 1.
Signature
mb_strrpos(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): int|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $haystack | string | The string being checked, for the last occurrence of needle |
| $needle | string | The string to find in haystack. |
| $offset | int | May be specified to begin searching an arbitrary number of characters into the haystack. Negative values will stop searching at an arbitrary point prior to the end of the haystack. |
| $encoding | ?string | The encoding parameter is the character encoding. If it is omitted or null, the internal character encoding value will be used. |
Return value
Returns the numeric position of the last occurrence of needle in the haystack string. If needle is not found, it returns false.
Example
The PHP manual ships no example for mb_strrpos(); the signature and the parameter table above are the contract.
Changelog
| Version | Change |
|---|---|
| 8.0.0 | needle now accepts an empty string. |
| 8.0.0 | Passing the encoding as the third argument instead of an offset has been removed. |
| 8.0.0 | encoding is nullable now. |
| 8.0.0 | Now throws a ValueError if encoding is an invalid encoding. Previously, an E_WARNING was emitted and false was returned. |
See also
mb_strpos(), mb_internal_encoding(), strrpos()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page mb_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.