欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php摘要生成函數(shù)(無亂碼)

 更新時間:2012年02月04日 16:05:42   作者:  
以前也寫過一個PHP文章摘要生成方法(函數(shù)), 不過,不怎么好用,也出現(xiàn)亂碼,現(xiàn)在再發(fā)布一個,這個函數(shù)是在某開源系統(tǒng)上拆下來了,希望對大家用用
在使用的時候,得先把要生成摘要的內(nèi)容strip_tags()一下,當(dāng)然,你也可以把strip_tags()直接添加到函數(shù)中,我沒有搞,自己添加吧。下面是函數(shù):
復(fù)制代碼 代碼如下:

function cutstr($string, $length,$charset,$dot) {//字符,截取長度,字符集,結(jié)尾符
if(strlen($string) <= $length) {
return $string;
}
$pre = chr(1);
$end = chr(1);
//保護特殊字符串
$string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), $string);
$strcut = '';
if(strtolower($charset) == 'utf-8') {
$n = $tn = $noc = 0;
while($n < strlen($string)) {
$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) {
$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;
}
}
if($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, 0, $n);
} else {
for($i = 0; $i < $length; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}
//還原特殊字符串
$strcut = str_replace(array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
//修復(fù)出現(xiàn)特殊字符串截段的問題
$pos = strrpos($s, chr(1));
if($pos !== false) {
$strcut = substr($s,0,$pos);
}
return $strcut.$dot;
}

相關(guān)文章

最新評論