datasn.io
datasn.io

Reputation: 12877

PHP preg_match_all fails on long strings

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)?

Upvotes: 4

Views: 3953

Answers (1)

Phil
Phil

Reputation: 164956

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.

Upvotes: 6

Related Questions