The mb_strstr Function
The mb_strstr function searches for the first occurrence of a substring in a string with multibyte encoding support.
The first parameter is the string to search in. The second parameter is the substring to search for.
The third optional parameter determines whether to return the part of the string before the found occurrence. The fourth optional parameter specifies the encoding.
Syntax
mb_strstr(
string $haystack,
string $needle,
[bool $before_needle = false],
[string $encoding],
);
Example
Find the first occurrence of a substring in a string:
<?php
$res = mb_strstr('abcde', 'b');
echo $res;
?>
Code execution result:
'bcde'
Example
Find the first occurrence of a substring in a string and return the part before the occurrence:
<?php
$res = mb_strstr('abcde', 'cd', true);
echo $res;
?>
Code execution result:
'ab'
Example
Search with UTF-8 encoding specified:
<?php
$res = mb_strstr('Привет мир', 'мир', false, 'UTF-8');
echo $res;
?>
Code execution result:
'мир'