码迷,mamicode.com
首页 > 其他好文 > 详细

正则表达式 preg_match 匹配中文

时间:2019-01-23 14:02:08      阅读:594      评论:0      收藏:0      [点我收藏+]

标签:xxx   出错   正则表达   fse   http   ...   offset   reg   har   

preg_match 匹配中文出错 
2010年01月06日 星期三 14:55 

错误提示: 

Warning: preg_match() [function.preg-match]: Compilation failed: PCRE does not support \L, \l, \N, \U, or \u at offset 2 in XXXXX on line 3 

错误原因: 

preg_match("/^[\u4e00-\u9fa5]{1,4}$/",$str); 

在使用上述代码匹配汉字时,出现如题错误。 

测试1: 

   preg_match("/^[\u4e00-\u9fa5]{1,4}/",$str); 

去掉结尾的“$”,错误依旧。 

测试2: 

preg_match("/^[\x4e00-\x9fa5]{1,4}/",$str); 

将u改为“x”,错误消失,但是匹配失败。 

测试3: 

preg_match("/^[\x{4e00}-\x{9fa5}]{1,4}/",$str); 

加上大括号,错误提示: 

<b>Warning</b>: preg_match() [<a href=‘function.preg-match‘>function.preg-match</a>]: Compilation failed: character value in \x{...} sequence is too large at offset 9 in <b>D:XXX.php</b> on line <b>25</b><br /> 
测试4: 

preg_match("/^[\x{4e00}-\x{9fa5}]{1,4}/u",$str); 

加入参数u,错误消失,匹配正确。 

另外一个方法:放弃使用preg_match函数,改用ereg(),直接解决~ 

============================= 

特别注意加小写的u

来源:http://fengweihong.iteye.com/blog/1133094

正则表达式 preg_match 匹配中文

标签:xxx   出错   正则表达   fse   http   ...   offset   reg   har   

原文地址:https://www.cnblogs.com/bluealine/p/10308330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!