虽然pcretest -C
在我的服务器中的结果表明pcre支持utf8,但是即使我输入匹配的模式,下面的代码总是返回false,并且似乎它不识别utf-8字符:
$pattern = '/^\x{06F0}?\x{06F9}\d{9}$/u';
if (!preg_match($pattern, $value)) { // $value is a function parameter
return false;
}
return true;
pcretest -C
的输出:
PCRE version 7.8 2008-09-05
Compiled with
UTF-8 support
Unicode properties support
Newline sequence is LF
\R matches all Unicode newlines
Internal link size = 2
POSIX malloc threshold = 10
Default match limit = 10000000
Default recursion depth limit = 10000000
Match recursion uses stack
PHP版本:5.3.2
此代码在我的localhost中按预期工作。
有什么建议吗?
在这里工作(注意html_entity_decode
的charset默认在PHP 5.4中更改为UTF-8):
$ cat a.php
<?php
$pattern = '/^\x{06F0}?\x{06F9}\d{9}$/u';
var_dump(preg_match($pattern, html_entity_decode('۹123456789')));
$ php a.php
int(1)
请注意,默认情况下,PHP不使用系统PCRE库(尽管很多发行版,出于显而易见的原因,使用系统PCRE库)。键入php -i
并查找PCRE部分以获取有关二进制文件使用的版本的更多信息。