Smarty's usage of truncate {$ d. title | truncate: 25... the display is just a truncate. it seems that the numbers, various characters, and Chinese characters are treated as one character, but after all, two digits occupy the space of a Chinese character, and there is a "... ", occupies three more characters. This leads to uneven levels after titles are listed. The multi-digit display is very short, and the multi-digit display is very long. How to make truncate think of smarty's use of truncate
{$ D. title | truncate: 25}
In this way, I want to display excessive titles...
Just truncate seems to treat numbers, various characters, and Chinese as one character.
However, after all, the number occupies two Chinese spaces, and the "..." at the end occupies three characters.
This leads to uneven levels after titles are listed.
The multi-digit display is very short, and the multi-digit display is very long.
How can we make truncate think that the number is also half a character?
Hope you can advise me! thank you!
------ Solution --------------------
Why not handle this in PHP? Bangding
------ Solution --------------------
Define a function to intercept Chinese characters.
------ Solution --------------------
Alas, 20 points
Save the following code as modifier. mb_truncate.php and put this php in the libs/plugins directory under smarty.
Usage: {$ d. title | mb_truncate: 25}
PHP code
function smarty_modifier_mb_truncate($string, $length = 80, $etc = '...', $charset='UTF-8', $break_words = false, $middle = false){ if ($length == 0) return ''; if (mb_strlen($string) > $length) { $length -= min($length, mb_strlen($etc)); if (!$break_words && !$middle) { $string = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length+1, $charset)); } if(!$middle) { return mb_substr($string, 0, $length, $charset) . $etc; } else { return mb_substr($string, 0, $length/2, $charset) . $etc . mb_substr($string, -$length/2, (mb_strlen($string)-$length/2), $charset); } } else { return $string; }}