I'm using preg_match_all for very long pattern.
when run the code, i got this error :
Warning: preg_match_all(): Compilation failed: regular expression is too large at offset 707830
After searching, I got the solution, so I should increase value of pcre.backtrack_limit
and pcre.recursion_limit
in php.ini
But after I increase the value and restart my apache, it still got the same problem. My PHP verison is 5.3.8
increasing the PCRE backtrack and recursion limit may fix the problem, but will still fail when the size of your data hits the new limit. (doesn't scale well with more data)
example:
<?php
// essential for huge PCREs
ini_set("pcre.backtrack_limit", "23001337");
ini_set("pcre.recursion_limit", "23001337");
// imagine your PCRE here...
?>
to really solve the underlying problem, you must optimize your expression and (if possible) split your complex expression into "parts" and move some logic to PHP. I hope you get the idea by reading the example .. instead of trying to find the sub-structure directly with a single PCRE, i demonstrate a more "iterative" approach going deeper and deeper into the structure using PHP. example:
<?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;
}
}