1.字符串长度
PHP获取中英文混合字符串长度的实现代码如下,1中文=1位,2英文=1位,可自行修改
/**
* PHP获取字符串中英文混合长度
* @param $str string 字符串
* @param $$charset string 编码
* @return 返回长度,1中文=1位,2英文=1位
*/
public function strLength($str,$charset='utf-8'){
if($charset=='utf-8'){
$str = iconv('utf-8','gb2312',$str);
}
$num = strlen($str);
$cnNum = 0;
for($i=0;$i<$num;$i++){
if (ord(substr($str,$i+1,1))>127){
$cnNum++;
$i++;
}
}
$enNum = $num-($cnNum*2);
$number = ($enNum/2)+$cnNum;
return ceil($number);
}
2.截取字符串函数
UTF8编码,在UTF8中,一个中文字符占3个字节
/**
* 截取字符串函数
* @param unknown $str
* @param number $start
* @param unknown $length
* @param string $charset
* @param string $suffix
*/
public function cc_msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){
if(function_exists("mb_substr")){
return mb_substr($str,$start,$length,$charset);
}elseif (function_exists('iconv_substr')){
return iconv_substr($str,$start,$length,$charset);
}
$re['utf-8'] = "/[/x01-/x7f]|[/xc2-/xdf][/x80-/xbf]|[/xe0-/xef][/x80-/xbf]{2}|[/xf0-/xff][/x80-/xbf]{3}/";
$re['gb2312'] = "/[/x01-/x7f]|[/xb0-/xf7][/xa0-/xfe]/";
$re['gbk'] = "/[/x01-/x7f]|[/x81-/xfe][/x40-/xfe]/";
$re['big5'] = "/[/x01-/x7f]|[/x81-/xfe]([/x40-/x7e]|/xa1-/xfe])/";
preg_match_all($re[$charset],$str,$match);
$slice = join("",array_slice($match[0],$start,$length));
if($suffix){
return $slice."…";
}else{
return $slice;
}
}
网友评论