1 /**
2 * 字符截取 支持UTF8/GBK
3 * @param $string
4 * @param $length
5 * @param $dot
6 */
7 public static function str_cut($string, $length, $dot = '...') {
8 $strlen = strlen($string);
9 if($strlen <= $length) return $string;
10 $string = str_replace(array(' ',' ', '&', '"', ''', '“', '”', '—', '<', '>', '·', '…'), array('∵',' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
11 $strcut = '';
12
13 $length = intval($length-strlen($dot)-$length/3);
14 $n = $tn = $noc = 0;
15 while($n < strlen($string)) {
16 $t = ord($string[$n]);
17 if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
18 $tn = 1; $n++; $noc++;
19 } elseif(194 <= $t && $t <= 223) {
20 $tn = 2; $n += 2; $noc += 2;
21 } elseif(224 <= $t && $t <= 239) {
22 $tn = 3; $n += 3; $noc += 2;
23 } elseif(240 <= $t && $t <= 247) {
24 $tn = 4; $n += 4; $noc += 2;
25 } elseif(248 <= $t && $t <= 251) {
26 $tn = 5; $n += 5; $noc += 2;
27 } elseif($t == 252 || $t == 253) {
28 $tn = 6; $n += 6; $noc += 2;
29 } else {
30 $n++;
31 }
32 if($noc >= $length) {
33 break;
34 }
35 }
36 if($noc > $length) {
37 $n -= $tn;
38 }
39 $strcut = substr($string, 0, $n);
40 $strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), array(' ', '&', '"', ''', '“', '”', '—', '<', '>', '·', '…'), $strcut);
41 return $strcut.$dot;
42 }
43 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\方法二
44
45 /*
46 Utf-8、gb2312都支持的汉字截取函数
47 cut_str(字符串, 截取长度, 开始长度, 编码);
48 编码默认为 utf-8
49 开始长度默认为 0
50 */
51 public static function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
52 {
53 if($code == 'UTF-8')
54 {
55 $pa ="/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
56 preg_match_all($pa, $string, $t_string);
57
58 if(count($t_string[0]) - $start > $sublen)
59 {
60 return join('', array_slice($t_string[0], $start, $sublen))."...";
61 }
62 return join('', array_slice($t_string[0], $start, $sublen));
63 }
64 else
65 {
66 $start = $start*2;
67 $sublen = $sublen*2;
68 $strlen = strlen($string);
69 $tmpstr = '';
70 for($i=0; $i<$strlen; $i++)
71 {
72 if($i>=$start && $i<($start+$sublen))
73 {
74 if(ord(substr($string, $i, 1))>129)
75 {
76 $tmpstr.= substr($string, $i, 2);
77 }
78 else
79 {
80 $tmpstr.= substr($string, $i, 1);
81 }
82 }
83 if(ord(substr($string, $i, 1))>129)
84 {
85 $i++;
86 }
87 }
88 if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
89 return $tmpstr;
90 }
91 }
92
93
94
95
96
97
98