欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-04-20 12:58:20
...
复制代码 代码如下:


//截取字符串长度。支持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 }
//兼容ucs-4编码
$i=0;//字节计数
$from_i=0;//开始截取的字节位置
$from_len=0;//开始截取的字符位置
$tag=true;//标志$from_len是否已经被赋值
for($temp_len=0;($temp_len-$from_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 $i++;
}
if($byte_code>191&&$byte_code $i+=2;
}
if($byte_code>223&&$byte_code $i+=3;
}
if($byte_code>239&&$byte_code $i+=4;
}
if($byte_code>248&&$byte_code $i+=5;
}
if($byte_code>252&&$byte_code $i+=6;
}
}
return iconv('utf-8',$code,substr($str,$from_i,$i-$from_i).$rear);
}

以上就介绍了g236 PHP 字符串编码截取函数(兼容utf-8和gb2312),包括了g236方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签: g236