[size=medium]PHP计算字符数[/size]
//计算字符数
private function countStr($str){
$cclen=0;
$asclen=strlen($str);
$ind=0;
$hascc=ereg("[xa1-xfe]",$str); #判断是否有汉字
$hasasc=ereg("[x01-xa0]",$str); #判断是否有ascii字符
if($hascc && !$hasasc) #只有汉字的情况
return strlen($str)/2;
if(!$hascc && $hasasc) #只有ascii字符的情况
return strlen($str);
for($ind=0;$ind<$asclen;$ind++) {
if(ord(substr($str,$ind,1))>0xa0){
$cclen++;
$ind++;
}else{
$cclen++;
}
}
return $cclen;
}