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