Go PHP judgment string is pure English, pure Chinese or Chinese-English mixed (GBK)

Source: Internet
Author: User
Tags vars

PHP determines whether a string is a Chinese (or English) method, except that the regular expression determines and splits the character to determine whether the value of the character is less than 128

There is a more special method.

use the Mb_strlen and strlen functions in PHP to determine
The method is simple: Use the above two functions to measure the return value of the character in the current encoding, and then compare the return value.
The return value is equal to pure English, pure digital, English number mixed;
The return value is unequal, and the strlen return value can be mb_strlen divisible by the pure Chinese character
The return value is not equal, and the Strlen return value is not divisible by mb_strlen into English or Chinese mixed rows

Take a look at the following example:

PHP code
  1. <?php
  2. $strarray [1] = "Hello";
  3. $strarray [2] = "123456";
  4. $strarray [3] = "123hello";
  5. $strarray [4] = "Hello";
  6. $strarray [5] = "123 Hello";
  7. $strarray [6] = "Hello hi";
  8. $strarray [7] = "123hello Hello";
  9. foreach ($strarray as $key,$value)
  10. {
  11. $x = Mb_strlen ($value,' gb2312 ');
  12. $y = strlen ($value);
  13. echo $strarray [$key].' <span style= "color: #ff0000;" > '. $x.' </span> <span style= ' color: #ff0000; " > '.   $y.' </span> ';
  14. }
  15. ?>

The result after the run is:
Hello 5 5
123456 6 6
123hello 8 8
Hello 2 4
123 Hello 5 7
Hello Hi 7 9
123hello Hello 10 12

Source: http://007blogchina.appspot.com/?p=130001

HP does not have a direct function to determine whether a string is pure English or pure Chinese and Chinese-English mixed and can only write its own function. In order to realize this function, it is necessary to understand the character set Chinese character coding placeholder, the current domestic more commonly used character sets are UTF8 and GBK.

UTF8 each Chinese character equals 3 length;

GBK each Chinese character equals 2 length;

Using the differences between Chinese characters and English, we can use Mb_strlen function and strlen function to calculate two sets of length numbers respectively, and then to determine the type of string according to the law.

UTF-8 instances

PHP code
  1. <?php
  2. /**
  3. * PHP judgment String Pure Chinese or plain English or Chinese-English mixed
  4. */
  5. echo ' <meta charset= ' utf-8 '/> ';
  6. function Utf8_str ($str) {
  7. $MB = Mb_strlen ($str,' utf-8 ');
  8. $st = strlen ($str);
  9. if ($st = =$mb)
  10. return ' plain English ';
  11. if ($st%$mb ==0 && $st%3==0)
  12. return ' pure Chinese characters ';
  13. return ' Chinese-English mix ';
  14. }
  15. $str = ' blog ';
  16. Echo ' string: <span style= "color:red" > ". $str.' </span> <span style= ' color:red ' > '. UTF8_STR ($str).'  </span> ';
  17. ?>

Gbk method

PHP code
  1. function Gbk_str ($str) {
  2. $MB = Mb_strlen ($str,' GBK ');
  3. $st = strlen ($str);
  4. if ($st = =$mb)
  5. return ' plain English ';
  6. if ($st%$mb ==0 && $st%2==0)
  7. return ' pure Chinese characters ';
  8. return ' Chinese-English mix ';
  9. }

Source: http://www.qttc.net/201207142.html

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.