【问题标题】:trying to understand preg_replace's behavior on large strings [duplicate]试图了解 preg_replace 在大字符串上的行为 [重复]
【发布时间】:2020-07-16 13:07:55
【问题描述】:
$str = str_repeat('a', 1024 * 1024);
//$str = str_repeat('a', 1024);
$temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1);

echo strlen($temp);

str_repeat('a', 1024)$temp 的长度为 1024,str_repeat('a', 1024 * 1024)$temp 的长度为 0。

我正在运行 PHP 7.4.3。

可能是什么问题?

【问题讨论】:

标签: php regex pcre


【解决方案1】:

你实际上有一个错误

Process exited with code 137

关于这个功能:

$temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1);

最后 $str 是 NULL 并且自动你的 strlen($temp); 实际上是 strlen(NULL); 给你 0


1024 * 1024 = 1048576

php.ini 中 pcre.backtrack_limit 的默认设置是 1000000

要解决这个问题,请在 php.ni 文件中更改此设置

pcre.backtrack_limit=1048577

【讨论】:

    【解决方案2】:

    在 php.ini 中设置了字符限制来处理正则表达式。默认为 100000。给定的字符串长度 1024 * 1024 = 1048576 超过了限制。改变

    pcre.backtrack_limit=1048577
    

    在 php.ini 中重启 apache 就可以工作了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 2013-12-22
      相关资源
      最近更新 更多