Home  >  Article  >  Backend Development  >  PHP count number of characters

PHP count number of characters

巴扎黑
巴扎黑Original
2016-11-22 09:38:101767browse

//计算字符数
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;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn