参考文章: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.


 

相关文章:

  • 2021-12-03
  • 2022-01-12
  • 2022-12-23
  • 2021-09-27
  • 2021-10-29
  • 2021-11-16
  • 2021-08-06
猜你喜欢
  • 2021-11-13
  • 2022-12-23
  • 2021-07-25
  • 2021-09-03
  • 2021-10-13
  • 2021-07-14
  • 2021-06-03
相关资源
相似解决方案