4

Here's my code:

$long = str_repeat('a very long string text', 100); // try changing 100 to 5000

$str = <<<STR
<abc>a short string text</abc>
<abc>$long</abc>
STR;

preg_match_all('@<abc>([^<>]+)</abc>@sU', $str, $matched);

print_r($matched);

And it works totally as expected. However, after you have changed 100 repetitions to 5000, run

print_r($matched);

And you will only get results for the short string occurrence.

My question is how to make preg_match or preg_match_all to work with large string texts (as large as 1MB or larger)?

datasn.io
  • 12,564
  • 28
  • 113
  • 154
  • Looks like you try to parse HTML/XML with regex. Don't do that. See http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 for the reasons why. – ThiefMaster Nov 15 '10 at 02:30

1 Answers1

6

You will probably need to increase the PCRE limits.

http://www.php.net/manual/en/pcre.configuration.php

Edit: But yeah, as ThiefMaster says, don't do this.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • These parameters are named `pcre.backtrack_limit` and `pcre.recursion_limit` and placed in php.ini. – soshial Jun 08 '12 at 15:41
  • It would be nice if the manual pages on the PCRE functions (e.g. http://php.net/manual/en/function.preg-match.php) would say, under "Parameters > `subject`", that if `subject` is longer than these limits, the result will be an error (i.e., return `false`), but not an exception that can be caught. – NewSites Oct 10 '18 at 16:26