中文英文字符串截取 指定个数

public static function arr_split_zh($tempaddtext)
{
$tempaddtext = iconv("UTF-8", "gb2312", $tempaddtext);
$cind = 0;
$arr_cont = [];

for($i = 0; $i < strlen($tempaddtext); $i++){
if (strlen(substr($tempaddtext, $cind, 1)) > 0){
if (ord(substr($tempaddtext, $cind, 1)) < 0xA1){ //如果为英文则取1个字节
array_push($arr_cont, substr($tempaddtext, $cind, 1));
$cind++;
}else{
array_push($arr_cont, substr($tempaddtext, $cind, 2));
$cind += 2;
}
}
}
foreach($arr_cont as &$row){
$row = iconv("gb2312", "UTF-8", $row);
}

return $arr_cont;

}


$array = self::arr_split_zh($content);
if (count($array) <= 54){
return $content;
}else{
$str = implode('', array_slice($array, 0, 54));
return $str;
}
posted @ 2016-10-21 15:58  秋风落叶.  阅读(287)  评论(0编辑  收藏  举报