在使用的時候,得先把要產生摘要的內容strip_tags()一下,當然,你也可以把strip_tags()直接加到函數中,我沒有搞,自己加吧。以下是函數:
複製程式碼 程式碼如下:
function cutstr($string, $length,$charset,$dot) {//字元,截取長度,字元集,結尾符if(strlen ($string) return $string;
}
$pre = chr(1);
$end = chr(1);
//保護特殊字串
$string = str_replace(array( '&', '"', ''), array($pre.'&'.$end, $pre.'"'.$end, $pre.''.$end), $string);
$strcut = '';
if(strtolower($charset) == 'utf-8') {
$n = $tn = $noc = 0;
while($n $t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 $tn = 1; $n++; $noc++;
} elseif(194 $tn = 2; $n += 2; $noc += 2;
} elseif(224 $tn = 3; $n += 3; $noc += 2;
} elseif(240 $tn = 4; $n += 4; $noc += 2;
} elseif(248 $tn = 5; $ n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6; $noc += 2;
} else {
$n++;
}
if($noc >= $length) {
break;
}
}
if($noc > $length) {
$n -= $tn;
}
($string, 0, $n);
} else {
for($i = 0; $i $strcut .= ord($string[$i]) > 127 ? $ string[$i].$string[++$i] : $string[$i];
}
}
//還原特殊字串
$strcut = str_replace(array($pre.'&'.$end , $pre.'"'.$end, $pre.''.$end), array('&', '"', '') , $strcut);
//修復出現特殊字串截段的問題
$pos = strrpos($s, chr(1));
if($pos !== false) {
$strcut = substr($ s,0,$pos);
}
return $strcut.$dot;
}