PHP PCRE 在测试和托管服务器上的差异
我的以下正则表达式在我的测试服务器上运行良好,但在我的托管服务器上仅返回一个空字符串。
$text = preg_replace('~[^\\pL\d]+~u', $use, $text);
现在我非常确定这归因于 PCRE 的托管服务器版本未在启用 Unicode 属性支持的情况下进行编译。这两个版本的差异如下:
我的服务器:
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
托管服务器:
PCRE version 4.5 01-December-2003
Compiled with
UTF-8 support
Newline character is LF
Internal link size = 2
POSIX malloc threshold = 10
Default match limit = 10000000
Match recursion uses stack
另请注意,托管服务器上的版本(编译 PHP 的版本相同)相当旧。
但 pcretest 在命令行上的两台服务器上都失败了
re> ~[^\\pL\d]+~u
** Unknown option 'u'
但令我困惑的是,虽然这个正则表达式在我的服务器上从 PHP 运行时工作正常,
。所以,我想我的问题是,正则表达式在托管服务器上是否会因为缺少 Unicode 属性而失败?还是我还缺少其他东西?
谢谢大家, 天然气。
I've got the following regular expression that works fine on my testing server, but just returns an empty string on my hosted server.
$text = preg_replace('~[^\\pL\d]+~u', $use, $text);
Now I'm pretty sure this comes down to the hosting server version of PCRE not being compiled with Unicode property support enabled. The differences in the two versions are as follows:
My server:
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
Hosting server:
PCRE version 4.5 01-December-2003
Compiled with
UTF-8 support
Newline character is LF
Internal link size = 2
POSIX malloc threshold = 10
Default match limit = 10000000
Match recursion uses stack
Also note that the version on the hosting server (the same version PHP is compiled against) is pretty old.
What confuses me though, is that pcretest fails on both servers from the command line with
re> ~[^\\pL\d]+~u
** Unknown option 'u'
although this regexp works fine when run from PHP on my server.
So, I guess my questions are does the regular expression fail on the hosting server because of the lack of Unicode properties? Or is there something else that I'm missing?
Thanks all,
Gaz.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PCRE 4.5 支持 UTF-8,但不支持 Unicode 属性,例如
\pL
,并且不支持启用它们的/u
标志。您需要将服务器上的 PCRE 升级到版本 5.0 或更高版本。您可能还需要升级 PHP 以支持preg_replace()
中的/u
。PCRE 4.5 does support UTF-8 but does not support Unicode properties such as
\pL
and does not support the/u
flag that enables them. You'll need to upgrade PCRE on your server to version 5.0 or later. You may also need to upgrade PHP to have support for/u
inpreg_replace()
.最有可能的是,主机服务器上的 PCRE 未启用 UTF8 支持。
pcretest 使用不同的选项。 UTF-8 的意思是“8”,而不是“u”。这就是错误的原因。
另请注意,不同的 PCRE 库可以在同一台计算机上的 Apache 或 CLI 版本的 PHP 中使用。
Most likely, the PCRE on the host server doesn't have UTF8 support enabled.
pcretest uses different options. It's '8' for UTF-8, instead of 'u'. That's the reason of the error.
Please also notice that different PCRE libraries can be used in Apache or CLI versions of PHP on the same machine.
回答您的问题;是的,也不是。我相信 PCRE 5.0 是 UTF8 支持可用的门槛。
To answer your questions; yes, and no. I believe PCRE 5.0 was the threshold when that UTF8 support became available.