PCRE does not support L, l, N{name}, U, or u...

PCRE does not support L, l, N{name}, U, or u...


参考文章:YCSUNNYLIFE 的 《php 正则匹配中文


一、报错情景:

使用preg_match()匹配中文时:

<?php


$str = 'china 中国 hello 你好';
preg_match('/[u4e00-u9fa5]/', $str, $arr);
var_dump($arr);

二:报错信息:

Warning: preg_match(): Compilation failed: PCRE does not support L, l, N{name}, U, or u at offset 2 in C:wampwwwindex.php on line 5

三、报错原因:

u(PCRE_UTF8)
此修正符启用了一个 PCRE 中与 Perl 不兼容的额外功能。
模式字符串被当成 UTF-8。
本修正符在 Unix 下自 PHP 4.1.0 起可用,在 win32 下自 PHP 4.2.3 起可用。
自 PHP 4.3.5 起开始检查模式的 UTF-8 合法性。


四、解决方法:

正则表达式用/[x{4e00}-x{9fa5}]/u.
注意:u换乘x,中间要有花括号{},结尾要有u.


原文地址:https://www.cnblogs.com/gyfluck/p/11690638.html