Home >Backend Development >PHP Tutorial >A small function to obtain the length of Chinese characters

A small function to obtain the length of Chinese characters

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 09:07:08942browse
  1. //Chinese character length, one Chinese character length is 1.

  2. function cstrlen($str){
  3. $n = 0; $p = 0; $c = ”;
  4. $len = strlen($str);

  5. for($i = 0 ; $i < $len; $i++) {

  6. $c = ord($str{$i});
  7. if($c > 127) {
  8. $p = 1;
  9. } else {
  10. $p = 0;
  11. }
  12. $i+=$p;$n++;
  13. }

  14. return $n;

  15. }
  16. ?>

Copy code


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