The mb_strrchr Function
The mb_strrchr function searches for the last occurrence of a character in a string
and returns the part of the string from that character to the end. Unlike strrchr,
it works with multibyte encodings (UTF-8, etc.). The first parameter
is the string to search in, the second is the character to search for, the third is an optional
parameter for searching before the character, and the fourth is the encoding.
Syntax
mb_strrchr(
string $haystack,
string $needle,
bool $before_needle = false,
string $encoding = null
);
Example
Find the last occurrence of a character in a string:
<?php
$res = mb_strrchr('abcba', 'b');
echo $res;
?>
Code execution result:
'ba'
Example
Find the part of the string before the last occurrence of the character 'b':
<?php
$res = mb_strrchr('abcba', 'b', true);
echo $res;
?>
Code execution result:
'abc'
Example
Working with Cyrillic in UTF-8:
<?php
$res = mb_strrchr('привет мир', 'и');
echo $res;
?>
Code execution result:
'ир'