Determines whether the input is a pure number, English or Chinese character.

Source: Internet
Author: User
Determines whether the input is a pure number, English or Chinese character.
Using the mb_strlen and strlen functions of php, you can easily know whether the string is composed of English letters, English letters and Chinese characters, or pure Chinese characters. A brief description is as follows (the preceding example program). 1. if the length returned by strlen is the same as that calculated by the current encoding, it can be determined that it is a pure English string. 2. if the length of the character returned by strlen is different from that calculated by mb_strlen based on the current encoding, and the return value of strlen is the same as that of mb_strlen, after which 0 is obtained, it can be regarded as a Chinese character string. 3. if the length of the character returned by strlen is different from that calculated by mb_strlen based on the current encoding, and the return value of strlen is the same as that of mb_strlen, the return value is not 0 After remainder, it can be determined to be an English-Chinese string.

  1. /********
  2. Determines whether the input is a pure number, English or Chinese character.
  3. Using the mb_strlen and strlen functions of php, you can easily understand the composition of strings.
  4. Whether it is English, English-Chinese, or pure Chinese characters. A brief description is as follows (the above example program)
  5. 1. if the length of the character returned by strlen and mb_strlen are calculated as the current encoding
  6. Can be determined to be a pure English string.
  7. 2. if the length of the character returned by strlen is inconsistent with that calculated by the current encoding,
  8. And the strlen return value is the same as the mb_strlen return value. after the remainder is obtained, 0 can be judged as a Chinese character string.
  9. 3. if the length of the character returned by strlen is inconsistent with that calculated by the current encoding,
  10. The return value of strlen is equal to that of mb_strlen. if the return value is not 0 after the remainder is obtained, it can be regarded as a string of English-Chinese mixture.
  11. *
  12. *****************/
  13. $ Str = "456abc ";
  14. $ X = mb_strlen ($ str, 'gb2312 ');
  15. $ Y = strlen ($ str); echo "------ 456abc ----
    ";
  16. Echo "$ x ".'
    ';
  17. Echo "$ y ".'
    ';
  18. $ Str = "456 I am a chinese abc
    ";
  19. $ X = mb_strlen ($ str, 'gb2312 ');
  20. $ Y = strlen ($ str );
  21. Echo "------ 456 I am a chinese abc ----
    ";
  22. Echo "$ x ".'
    '; Echo "$ y ".'
    ';
  23. $ Str = "I am a chinese and I love my motherland ";
  24. $ X = mb_strlen ($ str, 'gb2312 ');
  25. $ Y = strlen ($ str );
  26. Echo "------ I am a chinese, I love my motherland ----
    "; Echo" $ x ".'
    ';
  27. Echo "$ y ".'
    '; $ Str = "I ";
  28. $ X = mb_strlen ($ str, 'gb2312 ');
  29. $ Y = strlen ($ str );
  30. Echo "------ I ----
    "; Echo" $ x ".'
    ';
  31. Echo "$ y ".'
    ';
  32. $ Str = "my AB ";
  33. $ X = mb_strlen ($ str, 'gb2312 ');
  34. $ Y = strlen ($ str );
  35. Echo "------ I AB ----
    "; Echo" $ x ".'
    '; Echo "$ y ".'
    ';
  36. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.