首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么带有PCRE_UTF8的PHP preg_match_all在CLI和Apache/mod_php上会得到不同的结果?

为什么带有PCRE_UTF8的PHP preg_match_all在CLI和Apache/mod_php上会得到不同的结果?
EN

Stack Overflow用户
提问于 2013-10-25 19:20:05
回答 3查看 1.1K关注 0票数 3

以下代码在通过CLI和Apache/mod_php运行时会产生不同的结果:

代码语言:javascript
复制
<pre>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

echo setlocale(LC_ALL, 0)."\n";
// echo setlocale(LC_ALL, "en_GB.UTF-8")."\n";

$terms = array
(
    //Always matches:
    "Label Generation",
    //Doesn't match when using u (PCRE_UTF8) modifier:
    "Receipt of Prescription and Validation of Patient Information",

);

$text       = "Some terms to match: ".implode(", ",$terms);
$pattern    = "/(".implode(")|(", $terms).")/is";
$regexps    = array
(
   "Unicode"     => $pattern."u", //Add u (PCRE_UTF8) modifier
   "Non-unicode" => $pattern
);

echo "Text:\n'$text'\n";

foreach($regexps as $type=>$regexp)
{
    $matches    = array();
    $total      = preg_match_all($regexp,$text,$matches);

    echo "\n\n";
    echo "$type regex:\n'$regexp'\n\n";
    echo "Total $type matches: ";
    var_dump($total);
    echo "\n$type matches: ";
    var_dump($matches[0]);
}
?>
</pre>

CLI输出(正确):

代码语言:javascript
复制
<pre>
/en_GB.UTF-8/C/C/C/C/C
Text:
'Some terms to match: Label Generation, Receipt of Prescription and Validation of Patient Information'


Unicode regex:
'/(Label Generation)|(Receipt of Prescription and Validation of Patient Information)/isu'

Total Unicode matches: int(2)

Unicode matches: array(2) {
  [0]=>
  string(16) "Label Generation"
  [1]=>
  string(61) "Receipt of Prescription and Validation of Patient Information"
}


Non-unicode regex:
'/(Label Generation)|(Receipt of Prescription and Validation of Patient Information)/is'

Total Non-unicode matches: int(2)

Non-unicode matches: array(2) {
  [0]=>
  string(16) "Label Generation"
  [1]=>
  string(61) "Receipt of Prescription and Validation of Patient Information"
}
</pre>

Apache/mod_php webserver结果(不正确-不使用/u修饰符时仅匹配字符串):

代码语言:javascript
复制
/en_GB.ISO8859-1/C/C/C/C/C
Text:
'Some terms to match: Label Generation, Receipt of Prescription and Validation of Patient Information'


Unicode regex:
'/(Label Generation)|(Receipt of Prescription and Validation of Patient Information)/isu'

Total Unicode matches: int(1)

Unicode matches: array(1) {
  [0]=>
  string(16) "Label Generation"
}


Non-unicode regex:
'/(Label Generation)|(Receipt of Prescription and Validation of Patient Information)/is'

Total Non-unicode matches: int(2)

Non-unicode matches: array(2) {
  [0]=>
  string(16) "Label Generation"
  [1]=>
  string(61) "Receipt of Prescription and Validation of Patient Information"
}

使用/u (PCRE_UTF8)选项时,web服务器无法同时匹配两个字符串。我已经尝试过setlocale(LC_ALL, "en_GB.UTF-8");将web服务器的区域设置与CLI区域设置进行匹配,它成功地做到了这一点,但它对输出没有影响。我怀疑PCRE库有问题,但我不明白它在CLI和web服务器之间有什么不同- PHP在两个环境中报告相同的库版本:PHP5.4.14PCRE (Perl兼容正则表达式)支持启用=>的PCRE库版本=> 8.32 2012-11-30

pcretest报告不支持UTF-8,但尽管如此,CLI版本仍会产生正确的结果

代码语言:javascript
复制
$> pcretest -C
PCRE version 8.32 2012-11-30
Compiled with
  8-bit support
  No UTF-8 support
  No Unicode properties support
  No just-in-time compiler 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
EN

回答 3

Stack Overflow用户

发布于 2017-09-04 07:01:02

这个PHP设置对我很有帮助:

代码语言:javascript
复制
pcre.jit=0 
票数 3
EN

Stack Overflow用户

发布于 2014-04-23 09:21:05

阿拉斯泰尔,揭示了这个古老的问题,因为它涉及到一个永恒的问题的程序员感兴趣的各个时代。

正如Dino所说,在同一机器上有多个版本的PCRE是很常见的。我总是惊讶于一个普通的cPanel版本上安装了多少个版本的PCRE。这可能不是您的情况,但您似乎也有多个版本。

要查看安装了哪些PCRE,请在unix shell中键入:

代码语言:javascript
复制
find / -name libpcre.*

如果你想得到一些有意义的信息,你会想要使用你一直在做的pcretest,所以你可以find / -name pcretest然后somepath/pcretest -C

如果您使用的是cPanel、according to cPanel staff,则EasyApache安装的PCRE版本为opt/文件夹中的版本。您可以通过运行以下命令获取版本

代码语言:javascript
复制
/opt/pcre/bin/pcretest -C

它是一团糟,但这使我们保持警惕。:)

票数 2
EN

Stack Overflow用户

发布于 2013-10-26 05:14:16

一些Linux发行版(例如Ubuntu)将它们的PHP打包成单独的php.ini文件,用于CLI和Apache。如果这是您的情况,那么您可能需要查看/etc/php5并调查其中的差异。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19588406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档