The mb_strrpos Function
The mb_strrpos function finds the position of the last occurrence of a substring in a string. It works with multibyte encodings (UTF-8, etc.). Its first parameter is the string to search in, the second is the substring to find, the third (optional) is the starting position for the search, and the fourth (optional) is the encoding.
Syntax
mb_strrpos(
string $haystack,
string $needle,
int $offset = 0,
string $encoding
): int|false
Example
Let's find the position of the last occurrence of a substring in a string:
<?php
$res = mb_strrpos('аабабв', 'б');
echo $res;
?>
Code execution result:
3
Example
Search with UTF-8 encoding specified:
<?php
$res = mb_strrpos('日本語', '語', 0, 'UTF-8');
echo $res;
?>
Code execution result:
2
Example
When the substring is not found, the function returns false:
<?php
$res = mb_strrpos('abcde', 'z');
var_dump($res);
?>
Code execution result:
false