The mb_strcut Function
The mb_strcut
function is similar to substr
, but works correctly with multibyte encodings.
It accepts a string as the first parameter, the start position in characters (not bytes) as the second,
and the length of the substring to extract as the third. The fourth optional parameter can specify the encoding.
Syntax
mb_strcut(string, start, length, [encoding]);
Example
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
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_substr
function,
which also works with multibyte strings