Anybody run into this problem for complex Latin characters, such as Vietnamese?
function truncate($str, $length, $append = '…') {
$strLength = mb_strlen($str);
if ($strLength <= $length) {
return $str;
}
return mb_substr($str, 0, $length) . $append;
}
echo truncate('Bà Rịa - Vũng Tàu!', 14);
outputs:
Bà Rịa - V�…
http://codepad.viper-7.com/GOZFB0
I need some help to make it cut at the character, but I'm not even sure what is going on behind the scenes here.
You could use mb_strimwidth (PHP Documentation):
Or a custom function like Multibyte String Truncate for Smarty:
Be sure to perform necessary Unicode normalization to ensure each character correspond to a single codepoint.
Vietnamese Unicode FAQs