Home  >  Article  >  Backend Development  >  Determine whether the input is pure numbers, English, Chinese characters, etc.

Determine whether the input is pure numbers, English, Chinese characters, etc.

WBOY
WBOYOriginal
2016-07-25 09:11:391179browse
You can easily know the structure of the string by using php's mb_strlen and strlen functions. Is it all in English, a mixture of English and Chinese, or pure Chinese characters. A brief description is as follows (above sample program) 1. If the character length returned by strlen is the same as the length calculated by mb_strlen based on the current encoding Zhizhi, it can be judged to be a pure English string. 2. If the character length returned by strlen is inconsistent with the length calculated by mb_strlen based on the current encoding, And the return value of strlen is the same as the return value of mb_strlen. If the remainder is 0, it can be judged to be a string of all Chinese characters. 3. If the character length returned by strlen is inconsistent with the length calculated by mb_strlen based on the current encoding, And the return value of strlen is not 0 after the remainder of the return value of mb_strlen, it can be judged to be a mixed string of English and Chinese.
  1. /********
  2. Determine whether the input is pure numbers, English, Chinese characters, etc.
  3. Using php's mb_strlen and strlen functions, you can easily know the composition of the string
  4. whether it is all English, mixed English and Chinese, or pure Chinese characters. A brief description is as follows (above sample program)
  5. 1. If the character length returned by strlen is consistent with the length calculated by mb_strlen based on the current encoding, it can be judged to be a pure English string.
  6. 2. If the character length returned by strlen is inconsistent with the length calculated by mb_strlen based on the current encoding,
  7. and the remainder of the return value of strlen and the return value of mb_strlen is 0, it can be judged to be a string of all Chinese characters.
  8. 3. If the character length returned by strlen is inconsistent with the length calculated by mb_strlen based on the current encoding,
  9. and the return value of strlen is not 0 after the remainder of the return value of mb_strlen, it can be judged to be a mixed English-Chinese string.
  10. *
  11. * ****************/
  12. $str = "456abc";
  13. $x = mb_strlen($str,'gb2312');
  14. $y = strlen($str); echo "------456abc----
    ";
  15. echo "$x".'
    ';
  16. echo "$y".'
    ';
  17. $str = "456I am Chinese abc
    ";
  18. $x = mb_strlen($str,'gb2312');
  19. $y = strlen($str);
  20. echo "----- -456 I am Chinese abc----
    ";
  21. echo "$x".'
    '; echo "$y".'
    ';
  22. $str = "I am Chinese and I love my motherland";
  23. $x = mb_strlen($str,'gb2312');
  24. $y = strlen($str);
  25. echo "------I am China I love my motherland----
    "; echo "$x".'
    ';
  26. echo "$y".'
    '; $str = "I";
  27. $x = mb_strlen($str,'gb2312');
  28. $y = strlen($str);
  29. echo "------I----
    "; echo "$x".'
    ';
  30. echo "$y".'
    ';
  31. $str = "我ab";
  32. $x = mb_strlen($str,' gb2312');
  33. $y = strlen($str);
  34. echo "------I ab----
    "; echo "$x".'
    '; echo "$y".'
    ';
  35. ?>
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