加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – PHP preg_match_all限制

发布时间:2020-12-14 05:46:25 所属栏目:百科 来源:网络整理
导读:我正在使用preg_match_all非常长的模式. 运行代码时,我收到此错误: Warning: preg_match_all(): Compilation failed: regular expression is too large at offset 707830 搜索之后,我得到了解决方案,所以我应该在php.ini中增加pcre.backtrack_limit和pcre.r
我正在使用preg_match_all非常长的模式.

运行代码时,我收到此错误:

Warning: preg_match_all(): Compilation failed: regular expression is too large at offset 707830

搜索之后,我得到了解决方案,所以我应该在php.ini中增加pcre.backtrack_limit和pcre.recursion_limit的值

但是在我增加值并重启我的apache之后,它仍然遇到了同样的问题.我的PHP版本是5.3.8

解决方法

增加PCRE回溯和递归限制可能会解决问题,但是当数据大小达到新限制时仍会失败. (随着更多数据不能很好地扩展)

例:

<?php 
// essential for huge PCREs
ini_set("pcre.backtrack_limit","23001337");
ini_set("pcre.recursion_limit","23001337");
// imagine your PCRE here...
?>

要真正解决底层问题,必须优化表达式并(如果可能)将复杂表达式拆分为“部分”并将一些逻辑移到PHP.我希望你通过阅读这个例子得到这个想法..而不是试图用一个PCRE直接找到子结构,我展示了一种更加“迭代”的方法,使用PHP更深入地进入结构.例:

<?php
$html = file_get_contents("huge_input.html");

// first find all tables,and work on those later
$res = preg_match_all("!<table.*>(?P<content>.*)</table>!isU",$html,$table_matches);

if ($res) foreach($table_matches['content'] as $table_match) {  

    // now find all cells in each table that was found earlier ..
    $res = preg_match_all("!<td.*>(?P<content>.*)</td>!isU",$table_match,$cell_matches);

    if ($res) foreach($cell_matches['content'] as $cell_match) {

        // imagine going deeper and deeper into the structure here...
        echo "found a table cell! content: ",$cell_match;

    }    
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读