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
Release: 2016-07-25 09:07:08
Original
940 people have browsed it
  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


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