The mb_strcut Function
The mb_strcut function is similar to substr, but works correctly with multibyte encodings.
It takes a string as the first parameter, the starting position in characters (not bytes) as the second parameter,
and the length of the substring to extract as the third parameter. The fourth optional parameter can specify the encoding.
Syntax
mb_strcut(string, start, length, [encoding]);
Example
Let's extract 3 characters from a UTF-8 string, starting from position 1:
<?php
$str = 'Привет мир';
echo mb_strcut($str, 1, 3, 'UTF-8');
?>
Code execution result:
'рив'
Example
Let's extract a substring to the end of the string from Cyrillic text:
<?php
$str = 'Пример строки';
echo mb_strcut($str, 3, null, 'UTF-8');
?>
Code execution result:
'мер строки'
See Also
-
the
mb_substrfunction,
which also works with multibyte strings