The mb_strrichr Function
The mb_strrichr function performs a case-insensitive search for the last occurrence of a substring in a string. Unlike strrchr, it works with multibyte encodings (UTF-8, etc.). Returns the part of the string from the last found occurrence to the end of the string or false if the substring is not found.
Syntax
mb_strrichr(
string $haystack,
string $needle,
bool $before_needle = false,
string $encoding = null
): string|false
Example
Find the last occurrence of a substring in a string case-insensitively:
<?php
$res = mb_strrichr('aBcAbC', 'ab');
var_dump($res);
?>
Code execution result:
'AbC'
Example
Find the part of the string before the found occurrence:
<?php
$res = mb_strrichr('aBcAbC', 'ab', true);
var_dump($res);
?>
Code execution result:
'aBc'
Example
Example with UTF-8 encoding specified:
<?php
$res = mb_strrichr('ПриветМир', 'мир', false, 'UTF-8');
var_dump($res);
?>
Code execution result:
'Мир'
See Also
-
the
mb_strrchrfunction,
which searches for the last occurrence of a substring case-sensitively -
the
mb_striposfunction,
which searches for the position of the first occurrence of a substring case-insensitively -
the
mb_strstrfunction,
which searches for the first occurrence of a substring case-sensitively