This article mainly introduces the PHP custom function to achieve statistical Chinese string length method, combined with the case of a summary analysis of PHP for the Chinese judgment, coding and operation related operations skills, the need for friends can refer to the next
Chinese characters are calculated as 2 characters character characters calculated as 1
Code
/*** can count the Chinese string length function **/function abslength ($str) { $len =strlen ($STR); $i =0; while ($i < $len) { if (Preg_match ("/^[". Chr (0XA1). " -". Chr (0xff)." +$/", $str [$i]) { $i +=2; } else { $i +=1; } } return $i;}
Another: PHP judgment character length: Chinese, English, numbers.
There are many ways to do this. Record a simple.
Mb_strlen ($str, ' GBK ');
The disadvantage is to install MB libraries.
But there is still some problem to be solved.
The GB code rule is this: each character is composed of two bytes, the first byte range from 0xa1-0xfe, a total of 96 kinds. The second byte has a range of 96 0xa1-0xfe, respectively. A total of 96 * 96=8836 characters can be defined using these two bytes. There are actually 6,763 Chinese characters.
BIG5 Code encoding rule is this: each character is composed of two bytes, the first byte range from 0x81-0xfe, a total of 126 kinds. The second byte has a range of 157 0x40-0x7e,0xa1-0xfe, respectively. That is, a total of 126 * 157=19782 characters can be defined using these two bytes. Part of these Chinese characters are commonly used, such as, ding, these words we call characters commonly used, its BIG5 code range of 0xa440-0xc671, a total of 5,401. Less commonly used words, such as indiscriminate, tune, we call the second characters commonly used, the range of 0XC940-0XF9FE, a total of 7,652, the rest is some special characters.
Method of safe point.
function Strlenw ($str) { $count = 0; $len = strlen ($STR); for ($i =0; $i < $len; $i + +, $count + +) if (Ord ($str [$i]) >=128) $i + +; return $count;}
Finally, the following is the right, universal!
Code:
/** Effect: Statistical character length includes Chinese, English, number * Parameters: the string that needs to be counted, the encoding format is currently unified using utf-8* to modify records: $str = "KDS"; Echo Sstrlen ($str, ' utf-8 '); * */function Sstrlen ($str, $charset) { $n = 0; $p = 0; $c = "; $len = strlen ($STR); if ($charset = = ' Utf-8 ') {for ($i = 0; $i < $len; $i + +) { $c = Ord ($str {$i}); if ($c > 252) { $p = 5; } elseif ($c > 248) { $p = 4; } elseif ($c >) { $p = 3; } el Seif ($c > 224) { $p = 2; } elseif ($c > 192) { $p = 1; } else { $p = 0; } $i + = $p; $n + +; } } else {for ($i = 0; $i < $len; $i + +) { $c = Ord ($str {$i}); if ($c > 127) { $p = 1; } else { $p = 0; } $i + = $p; $n + +; } } return $n;}
The above is the whole content of this article, I hope that everyone's study has helped.