Home  >  Article  >  php教程  >  PHP中用正则表达式验证中文的问题

PHP中用正则表达式验证中文的问题

WBOY
WBOYOriginal
2016-06-21 09:00:49968browse

$str = '中华人民共和国123456789abcdefg';
echo preg_match("/^[\u4e00-\u9fa5_a-zA-Z0-9]{3,15}$",$strName);

运行一下上面这段代码,看会有什么提示信息?
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

原来,PHP正则表达式中不支持下列 Perl 转义序列:\L, \l, \N, \P, \p, \U, \u, or \X

在 UTF-8 模式下,允许用“\x{...}”,花括号中的内容是表示十六进制数字的字符串。原来的十六进制转义序列 \xhh 如果其值大于 127 的话则匹配了一个双字节 UTF-8 字符。
所以,可以这样来解决preg_match("/^[\x80-\xff_a-zA-Z0-9]{3,15}$",$strName);



Statement:
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