1 function smarty_modifier_truncate_cn_utf8($string, $length, $etc = '...')
2 {
3 $result = '';
4 $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
5 $strlen = strlen($string);
6
7 for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
8 {
9 if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
10 {
11 if ($length < 1.0)
12 {
13 break;
14 }
15
16 $result .= substr($string, $i, $number);
17 $length -= 1.0;
18 $i += $number - 1;
19 }
20 else
21 {
22 $result .= substr($string, $i, 1);
23 $length -= 0.5;
24 }
25 }
26
27 $result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
28
29 if ($i < $strlen)
30 {
31 $result .= $etc;
32 }
33
34 return $result;
35 }