<?php
$pattern = "/[\x{4e00}-\x{9fa5}]/u";//用来匹配utf-8的中文。
$subject ="PHP好不好";
preg_match($pattern, $subject, $result);
print_r($result);//结果:Array ( [0] => 好 )
?>
请问这里的{}是什么用法?为什么要这么用?
这和PHP无关,这是正则表达式的事
在这个场景下,是用16进制表示出UTF8中汉字编码的范围
供参考: http://www.regexlab.com/zh/deelx/syntax.htm , http://www.jb51.net/tools/zhengze.html ,
这是故意黑php呢?
很容易就可以找到啊http://www.regular-expressions.info/quickstart.html
Literal Characters段,Non-Printable Characters段都有说明这种使用方法
If your application supports Unicode, use \uFFFF or \x{FFFF} to insert a Unicode character. \u20AC or \x{20AC} matches the euro currency sign.
这里的{} 不是PHP语法里面的{},这个明显是匹配某个字符集的字符,和字符集相关!!!