Php judges that the string is in pure English, pure Chinese characters, or a mix of Chinese and English formats.

Source: Internet
Author: User
Php judges that the string is in pure English, pure Chinese characters, or a mix of Chinese and English formats.

  1. $ Strarray [1] = "hello ";
  2. $ Strarray [2] = "123456 ";
  3. $ Strarray [3] = "123 hello ";
  4. $ Strarray [4] = "script School ";
  5. $ Strarray [5] = "123 programmer's House ";
  6. $ Strarray [6] = "hello programmer's House ";
  7. $ Strarray [7] = "123hello programmer's House ";
  8. Foreach ($ strarray as $ key-> $ value)
  9. {
  10. $ X = mb_strlen ($ value, 'gb2312 ');
  11. $ Y = strlen ($ value );
  12. Echo $ strarray [$ key]. ''. $ x.''. $ y .'';
  13. }
  14. ?>

Running results: hello 5 5123456 6 6123 hello 8 Programmer's House 2 4123 programmer's house 5 7hello programmer's house 7 9123hello programmer's house 10 12

Php does not have a direct function to determine whether a string is pure English, pure Chinese characters, and a mix of Chinese and English characters. you can only write functions by yourself. To implement this function, you must understand the character set encoding placeholder. Currently, UTF8 and GBK are commonly used character sets in China. UTF8 each Chinese character is equal to three lengths; GBK each Chinese character is equal to two lengths;

Using the differences between Chinese characters and English letters, we can use the mb_strlen function and the strlen function to calculate the two groups of length numbers, and then calculate the string type based on the regular operation.

UTF-8 instance

  1. /**
  2. * PHP judges whether the string is pure Chinese characters OR only English OR a mix of Chinese and English characters
  3. */
  4. Echo' ';
  5. Function utf8_str ($ str ){
  6. $ Mb = mb_strlen ($ str, 'utf-8 ');
  7. $ St = strlen ($ str );
  8. If ($ st = $ mb)
  9. Return 'English only ';
  10. If ($ st % $ mb = 0 & $ st % 3 = 0)
  11. Return 'Chinese characters ';
  12. Return 'Chinese-English mixed ';
  13. }
  14. $ Str = 'blog ';
  15. Echo 'string: '. $ str.', which is '. utf8_str ($ str ).'';
  16. ?>

GBK method

  1. Function gbk_str ($ str ){
  2. $ Mb = mb_strlen ($ str, 'gbk ');
  3. $ St = strlen ($ str );
  4. If ($ st = $ mb)
  5. Return 'English only ';
  6. If ($ st % $ mb = 0 & $ st % 2 = 0)
  7. Return 'Chinese characters ';
  8. Return 'Chinese-English mixed ';
  9. }
  10. ?>

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.