Solve the problem of verifying Chinese characters using php regular expressions

Source: Internet
Author: User
Tags php regular expression
Solve the problem of verifying Chinese characters using php regular expressions

  1. $ Str = '123456789abcdefg of the People's Republic of China ';
  2. Echo preg_match ("/^ [\ u4e00-\ u9fa5_a-zA-Z0-9] {} $", $ strName );
  3. ?>

Run the above code and the prompt will be: Warning: preg_match (): Compilation failed: PCRE does not support \ L, \ l, \ N, \ P, \ p, \ U, \ u, or \ X at offset 3 in F: \ wwwroot \ php \ test. php on line 2

The reason is that the following Perl escape sequences are not supported in PHP regular expressions: \ L, \ l, \ N, \ P, \ p, \ U, \ u, or \ X

In UTF-8 mode, '\ x {...}' is allowed, and the content in braces is a string that represents a hexadecimal number. The original hexadecimal escape sequence \ xhh matches a dubyte UTF-8 character if its value is greater than 127.

Solution: preg_match ("/^ [\ x80-\ xff_a-zA-Z0-9] {} $", $ strName );

For example:

  1. /**
  2. * Php regular expression to verify Chinese characters
  3. * Edit bbs.it-home.org
  4. */
  5. $ Shouji = "hahaha ";
  6. If (! Preg_match ("/^ [\ x80-\ xff] {6, 30} $/", $ shouji )){
  7. Echo "nonono ";
  8. }
  9. Else {
  10. Echo "yesyesyes ";
  11. }
  12. ?>

Related Article

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.