Compilation failed: character value in \x{...} sequence is too large at offset xxx
当我们使用正则表达式匹配汉字时,收到一条警告信息,如:
<?php echo preg_replace("#[^\x{4e00}-\x{9fa5}]#", "", '爱E族:aiezu.com'); //抛出警告: // PHP Warning: preg_replace(): Compilation failed: character value in \x{...} sequence is too large at offset 9 in /tmp/aiezu.com.php on line 2这是由于我们正则表达式没加"u"模式原因,如果没加"u"模式,"\x"后的大括号中最多只能包含两个字符,范围为:"\x{00}-\x{ff}",表示匹配ascii字符,所以上面错误的解决方法就是加上"u"模式,修改后代码如下:
<?php echo preg_replace("#[^\x{4e00}-\x{9fa5}]#u", "", '爱E族:aiezu.com'); //输出: 爱族