PHP count number of characters

巴扎黑
Release: 2016-11-22 09:38:10
Original
1775 people have browsed it

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template