当前位置:Gxlcms > PHP教程 > PHP字符串编码截取函数(兼容utf-8和gb2312)_php实例

PHP字符串编码截取函数(兼容utf-8和gb2312)_php实例

时间:2021-07-01 10:21:17 帮助过:18人阅读

代码如下:
//截取字符串长度。支持utf-8和gb2312编码。若为gb2312,先将其转为utf-8,在utf-8的基础上截取然后再转换回来
function cut_string($str,$from=1,$length=10,$code='utf-8',$rear='...'){
if($code!='utf-8'){//总是将字符串转为utf-8编码
$str=iconv($code,'utf-8',$str);
}
$str_len=mb_strlen($str,'utf-8');//字符串的长度
if($from>$str_len){//如果截取开始位置大于字符串长度,截取后面$length个
$from=$str_len-$length+1;
$from=($from<1?1:$from);
}
//兼容ucs-4编码
$i=0;//字节计数
$from_i=0;//开始截取的字节位置
$from_len=0;//开始截取的字符位置
$tag=true;//标志$from_len是否已经被赋值
for($temp_len=0;($temp_len-$from_len<$length)||$tag;$temp_len++){
$byte_code=ord(substr($str,$i,1));//一个字节的编码
if($temp_len+1==$from){//记录开始截取的开始字节位置
$from_i=$i;$from_len=$temp_len;$tag=false;
}
if($byte_code>=0&&$byte_code<128){//字符是占用几个字节,utf-8是变长编码,根据每个字符的第一个字节可判断出该字符占几个字节
$i++;
}
if($byte_code>191&&$byte_code<224){
$i+=2;
}
if($byte_code>223&&$byte_code<240){
$i+=3;
}
if($byte_code>239&&$byte_code<248){
$i+=4;
}
if($byte_code>248&&$byte_code<252){
$i+=5;
}
if($byte_code>252&&$byte_code<255){
$i+=6;
}
}
return iconv('utf-8',$code,substr($str,$from_i,$i-$from_i).$rear);
}

人气教程排行