【问题标题】:PHP preg_match stops responding at certain number of characters in stringPHP preg_match 在字符串中的特定数量的字符处停止响应
【发布时间】:2015-05-16 14:14:21
【问题描述】:

是否有某种 PHP 配置设置会影响传递到 preg_match 或 preg_match_all 的字符串长度?

我正在尝试测试以下内容以找出对我来说出了什么问题:

    $re = "/\[\[section\:(.*?)\]\]((.|\n)*?)\[\[endsection\]\]/"; 
    $str = "[[section:body]]
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    lots of lines here
    [[endsection]]

    other stuff here"; 

    preg_match_all($re, $str, $matches);
    var_dump($matches);

现在如果我运行它没问题,它会立即返回结果。

但是,如果我在 $str 变量中再添加一行并再次运行它,它只会旋转和旋转,然后我无法显示网页。

但是,如果我在 writecodeonline.com/php 上运行完全相同的东西,它可以正常工作并立即返回。

我的 php 错误日志或 apache 错误日志中没有任何错误,它只是在遇到特定字符时拒绝执行任何操作(似乎是在遇到 707 strong> 停止响应的字符。

以前有人遇到过这种问题吗?只是我需要更改一些配置设置吗?

干杯。

【问题讨论】:

  • 要补充这一点,下面建议的 php.ini 设置没有帮助。同样的问题仍在发生。我已经尝试对脚本进行 webgrind 配置文件以查看发生了什么,但是一旦它达到字符串中的 695 个字符,它甚至不会创建配置文件输出,它是空的,并且脚本什么都不做,除了坐在那里旋转大约20秒左右。然而从字符串中删除一个字符,结果是即时的。
  • 这似乎是完全相同的问题:forum.wampserver.com/read.php?2,65936,65946 我也在 WAMP 服务器上,我的问题与那些人的行为完全相同
  • 使用以下信息解决了这个问题:stackoverflow.com/questions/7620910/… 基本上 pcre.recursion_limit 的默认设置 100000 太高,导致(我认为)堆栈溢出,导致 apache 崩溃并导致它重新启动,这解释了我的 apache 错误日志中没有错误,但重新启动通知。根据该问题中的说明,我将 WAMP 安装更改为 524。

标签: php regex preg-match


【解决方案1】:

是的,有the backtrack-limit

您可以在 php.ini 文件中进行配置。

说实话,一个快速的google search 会解决你的问题...

【讨论】:

  • 我已确保在 php.ini 中将该设置设置为 100000,但仍然出现同样的问题。
  • 现在它似乎一旦达到 695 个字符就停止工作了。
  • 我现在已经解决了这个问题,使用在这个问题中找到的信息:stackoverflow.com/questions/7620910/…
  • @ConnWarwicker 您能否详细说明该问题中的哪些信息可以帮助您解决问题?对于以后遇到这种情况的人来说可能是一个很好的参考。谢谢。
  • 我已将其添加为对我的问题的评论。
猜你喜欢
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多