The mb_strimwidth Function
The mb_strimwidth
function trims a string to a given width, considering multibyte characters. It accepts the source string as the first parameter, the start position as the second, and the maximum string width in characters as the third. An optional fourth parameter allows you to specify a string that will be appended to the end of the trimmed string.
Syntax
mb_strimwidth(string, start, width, [trimmarker], [encoding]);
Example
Trim the string to 6
characters:
<?php
$res = mb_strimwidth("Привет мир", 0, 6);
echo $res;
?>
Code execution result:
'Привет'
Example
Trim the string adding an ellipsis at the end:
<?php
$res = mb_strimwidth("Привет мир", 0, 8, "...");
echo $res;
?>
Code execution result:
'Привет...'
Example
Trim the string from the middle:
<?php
$res = mb_strimwidth("Привет мир", 3, 5);
echo $res;
?>
Code execution result:
'вет м'