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:\wamp\www\index.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.