Function name: mb_strrpos()
Applicable version: PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8
Usage: The mb_strrpos() function is used to find the last occurrence of a specified character or substring in a string. This function is a multibyte-safe version of strrpos() function provided by the mbstring extension.
Syntax: mb_strrpos(string $haystack, string $needle, int $offset = 0, string $encoding = null): int|false
parameter:
- $haystack (required): The original string in which to look for the substring.
- $needle (required): The substring to be found.
- $offset (optional): Specifies the offset to start the search. If an offset is set, the search starts at the specified position of the string. If the offset is a positive number, it indicates the position calculated from the beginning of the string; if it is a negative number, it indicates the position calculated from the end of the string.
- $encoding (optional): Specifies the character encoding to use. If not set, internal character encoding is used.
Return value: Returns the last occurrence position, and returns false if no substring is found.
Example:
$str = 'Hello, World! I love PHP.'; $pos = mb_strrpos($str, 'o'); echo $pos; // 输出:17 $pos = mb_strrpos($str, 'o', -10); echo $pos; // 输出:8 $pos = mb_strrpos($str, 'PHP'); echo $pos; // 输出:17 $pos = mb_strrpos($str, 'o', 10, 'UTF-8'); echo $pos; // 输出:8
illustrate:
- In the example, the first mb_strrpos() function call looks for the position of the last letter 'o' in the string and prints the result.
- The second mb_strrpos() function call starts from the end of the string, finds the position of the last letter 'o', and prints the result.
- The third mb_strrpos() function call looks for the last substring 'PHP' in the string and prints the result.
- The fourth mb_strrpos() function call is encoded using UTF-8 and starts to find the position of the last letter 'o' at the specified offset position and prints the result.