truncate_middle: make it utf8 aware

This commit is contained in:
Andrew Dolgov 2018-12-24 12:44:10 +03:00
parent 215c9f0f88
commit 1351ce370a
1 changed files with 11 additions and 3 deletions

View File

@ -863,10 +863,18 @@
}
}
// is not utf8 clean
function mb_substr_replace($original, $replacement, $position, $length) {
$startString = mb_substr($original, 0, $position, "UTF-8");
$endString = mb_substr($original, $position + $length, mb_strlen($original), "UTF-8");
$out = $startString . $replacement . $endString;
return $out;
}
function truncate_middle($str, $max_len, $suffix = '…') {
if (strlen($str) > $max_len) {
return substr_replace($str, $suffix, $max_len / 2, mb_strlen($str) - $max_len);
if (mb_strlen($str) > $max_len) {
return mb_substr_replace($str, $suffix, $max_len / 2, mb_strlen($str) - $max_len);
} else {
return $str;
}