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

中文在不同编码中占有的字节数和PHP截取

2015-10-23 19:41 633 查看
英文字母:

字节数 : 1;编码:GB2312

字节数 : 1;编码:GBK

字节数 : 1;编码:GB18030

字节数 : 1;编码:ISO-8859-1

字节数 : 1;编码:UTF-8

字节数 : 4;编码:UTF-16

字节数 : 2;编码:UTF-16BE

字节数 : 2;编码:UTF-16LE

 

中文汉字:

字节数 : 2;编码:GB2312

字节数 : 2;编码:GBK

字节数 : 2;编码:GB18030

字节数 : 1;编码:ISO-8859-1

字节数 : 3;编码:UTF-8

字节数 : 4;编码:UTF-16

字节数 : 2;编码:UTF-16BE

字节数 : 2;编码:UTF-16LE
utf-8下英文为1个字节,中文为3个字节;gbk英文为1个字节,中文为2个字节;

function msubstr($string,$length=0,$ellipsis='…',$start=0){
$string=strip_tags($string);
$string=preg_replace('/\n/is','',$string);
//$string=preg_replace('/ | /is','',$string);//清除字符串中的空格
$string=preg_replace('/ /is','',$string);
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,$string);
if(is_array($string)&&!empty($string[0])){
$string=implode('',$string[0]);
if(strlen($string)<$start+1){
return '';
}
preg_match_all("/./su",$string,$ar);
$string2='';
$tstr='';
//www.phpernote.com
for($i=0;isset($ar[0][$i]);$i++){
if(strlen($tstr)<$start){
$tstr.=$ar[0][$i];
}else{
if(strlen($string2)<$length+strlen($ar[0][$i])){
$string2.=$ar[0][$i];
}else{
break;
}
}
}
return $string==$string2?$string2:$string2.$ellipsis;
}else{
$string='';
}
return $string;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: