preg_match_all的`u`标志依赖于什么?

6

我有一些PHP应用程序的代码,在生产服务器上使用时返回null,但在开发服务器上可以正常工作。以下是代码行:

// use the regex unicode support to separate the UTF-8 characters into an array
preg_match_all( '/./us', $str, $match );

“u”标志依赖于什么?我测试了启用和禁用“mb_string”,似乎没有影响。

我收到的错误是:

preg_match_all: Compilation failed: unknown option bit(s) set at offset -1

更多信息

这是生产服务器上的选项之一:

'--with-pcre-regex=/opt/pcre'

这里是pcre部分

Picture.png

我相信这是@Wesley提到的那个注释:

In  order  process  UTF-8 strings, you must build PCRE to include UTF-8
support in the code, and, in addition,  you  must  call  pcre_compile()
with  the  PCRE_UTF8  option  flag,  or the pattern must start with the
sequence (*UTF8). When either of these is the case,  both  the  pattern
and  any  subject  strings  that  are matched against it are treated as
UTF-8 strings instead of strings of 1-byte characters.

有没有关于如何“构建包含UTF-8的PCRE”的链接或提示?

通过

pcretest -C的结果

PCRE version 6.6 06-Feb-2006
Compiled with
  UTF-8 support
  Unicode properties support
  Newline character is LF
  Internal link size = 2
  POSIX malloc threshold = 10
  Default match limit = 10000000
  Default recursion depth limit = 10000000
  Match recursion uses stack

你是在对变量还是常量进行测试?你的例子中有一个变量,我认为你应该对常量进行测试,以确保在开发和生产环境中执行相同的操作。 - hakre
1
请查看http://php.net/manual/en/reference.pcre.pattern.modifiers.php,注意评论。 - Wesley Murch
2个回答

5
这个标志取决于使用启用unicode支持的PCRE编译。
PHP捆绑了这个库,通常会启用unicode支持:自从PHP 4.1.0以来,当PHP使用捆绑的PCRE库进行编译时,u修饰符可用且始终有效。
然而,一些Linux发行版使用自己构建的PCRE来构建PHP,这些构建没有启用unicode支持,因此在这些构建上,u修饰符无法工作。
解决方案是使用替代的PHP软件包。

我应该查找什么来确定它是否已启用,我应该使用什么语言告诉系统管理员我需要它可用?我相信该系统基于CentOS。 - cwd
你可以在 /usr/include/pcre.h 中查看 PCRE_UTF8 宏是否被定义。 - Arnaud Le Blanc
我在生产机器上没有root访问权限,但我可以进入/usr/include,而pcre.h不在那里。同时,我已经更新了问题并提供了更多信息。 - cwd
将结果添加到问题中。看起来与该错误报告不同,因为它声称具有“Unicode属性支持”。 - cwd
也许 PHP 与系统上的另一个 PCRE 相关联;尝试运行 ldd /usr/bin/php 并查看它链接到哪个 libpcre。 - Arnaud Le Blanc
使用命令ldd /usr/bin/php | grep pcre查询到libpcre.so.0 => /lib/libpcre.so.0。不确定如何处理。如果执行/lib/libpcre.so.0,会提示Segmentation fault - cwd

1

这取决于 PCRE 是否使用 --enable-utf8 进行编译。


这听起来不错 - 你能详细说明一下吗?我在开发或生产机器的php_info中没有看到这个。 - cwd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接