您的位置:首页 > 编程语言 > PHP开发

php截取中文字符串 GB2312 utf-8

2014-07-31 15:21 495 查看
UTF-8截取中文字符串
function Cn_Substr($string, $length) {
preg_match_all("/[\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]/", $string, $info);
for($i=0; $i<count($info[0]); $i++) {
$wordscut .= $info[0][$i];
$j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
if ($j > $length - 3) {
return $wordscut." ...";
}
}
return join('', $info[0]);
}
GB2312截取中文字符串
function Cn_Substrs($strs,$lens){
if (strlen($strs)<$lens) {
$nstrs=$strs;
}else{
$mlens=$lens;
for ($i=0;$i<=strlen($strs) && $mlens>0;$i++){
$chars=substr($strs,$i,1);
if(ord($chars)>0xa0 ) {
$i++;
$mlens -=2;;
}else{
$mlens--;
}
}
$nstrs=substr($strs,0,$i)."...";
}
return $nstrs;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: