好用的PHP截取中文字串不乱码的函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* 截取字符串
* $string 原字符串
* $length 需要保留的字符长度
* $rephtml 是否保留字符串中的 HTML 标签 0为不保留1为保留
* $dot 截取后的连接符号
* $pagechar 字符串编码
* */
function str_cut($string,$length,$rephtml=0,$dot='...',$pagechar='utf8'){
//保留转译后的HTML字符
$rep_str1 = array(' ','&','"','<','>',''','“','”','·','—');
$rep_str2 = array(' ','&','"','<','>',"'","“","”","·","—");
if($rephtml==0) {
$string = htmlspecialchars_decode($string);
$string = str_replace($rep_str1 , $rep_str2, $string);
$string = strip_tags($string);
}
$string = preg_replace("/\s/", " ", $string);
$string = preg_replace("/^ +| +$/", '', $string);
$string = preg_replace("/ {2,}/is", " ", $string);
$strlen = strlen($string);
if($strlen<=$length){
return $string;
}
$strcut = '';
if($pagechar == 'utf8') {
$n = $tn = $noc = 0;
while($n < $strlen) {
$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1; $n++; $noc++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2; $n += 2; $noc += 2;
} elseif(224 <= $t && $t < 239) { // 239 中文逗号 ASCII编码
$tn = 3; $n += 3; $noc += 2;
} elseif(240 <= $t && $t <= 247) {
$tn = 4; $n += 4; $noc += 2;
} elseif(248 <= $t && $t <= 251) {
$tn = 5; $n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6; $noc += 2;
} else {
$n++;
}
if($noc >= $length) { break; $isdot = 1; }
}
if($noc > $length) { $n -= $tn; }
$strcut = substr($string, 0, $n);
if ($n < $strlen) {
$strcut = $strcut.$dot;
}
} else {
for($i = 0; $i < $length; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}
if($rephtml==0){
$string = str_replace($rep_str2, $rep_str1, $string);
}
return $strcut;
}