Home > Backend Development > PHP Tutorial > Solve the problem of Chinese php regular expression verification

Solve the problem of Chinese php regular expression verification

WBOY
Release: 2016-07-25 08:59:10
Original
1124 people have browsed it
  1. $str = 'People's Republic of China 123456789abcdefg';
  2. echo preg_match("/^[u4e00-u9fa5_a-zA-Z0-9]{3,15}$",$strName);
  3. ?>
Copy the code

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

The reason is because: PHP regular expressions do not support the following Perl escape sequences: L, l, N, P, p, U, u, or X

In UTF-8 mode, "x{...}" is allowed, and the content in the curly brackets is a string representing a hexadecimal number. The original hexadecimal escape sequence xhh matches a double-byte UTF-8 character if its value is greater than 127.

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

For example, the following example:

  1. /**
  2. * php regular expression verification Chinese
  3. * edit bbs.it-home.org
  4. */
  5. $shouji = "Hahahaha";
  6. if (!preg_match("/^[x80-xff]{6,30}$/" ,$shouji)){
  7. echo "nonono";
  8. }
  9. else {
  10. echo "yesyesyes";
  11. }
  12. ?>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template