Reputation: 26075
I find that if I try to use preg_replace on a very long string, PHP returns an empty page to my browser without showing an error message. I was able to reproduce this bug in my testing environment. What is the maximum length of a string that preg_replace can handle? Is it possible to increase this length?
Upvotes: 6
Views: 4483
Reputation: 1082
Default value for pcre.backtrack_limit is 1000000. If your string length is above that value you can easily solve it by adding this to the beginning of your script:
ini_set('pcre.backtrack_limit', 5000000);
In the code above the max length of the string has been set to 5000000. Adjust based on your own needs.
Upvotes: 0
Reputation: 81
The same happened to me for $pattern matching string over 4M. Probably you will have to increase pcre.backtrack_limit using ini_set() or editing php.ini.
Check preg for last error:
$retval = preg_replace ($pattern, $replacement, $subject);
if ($retval === null) {
// see http://php.net/manual/en/function.preg-last-error.php
echo preg_last_error();
}
Upvotes: 8
Reputation: 1
I met this problem too, using this regex (i found it somewhere i don't remember):
'~\[quote(?:=([^\]]+))?\]((?:(?R)|.)*?)\[/quote\]~s'
This is used to replace nested quote BBcodes, using the (?R) recursive pattern modifier.
When the string between quotes approaches 300 characters (that is really few), I get a "Reset connexion" in my browser. Nothing in the error logs of Apache or PHP (I am running Wampserver 2.4 with PHP 5.4.16).
It seems that (?R) is an experimental facility. On this post : PHP, nested templates in preg_replace, there is a regex with (?R) that is explained in details, which lead me to a solution that works on much longer strings :
'%\[quote(?:=([^\]]+))?\]((?:[^[]*(?:\[(?!/?quote(?:=[^\]]+)?\])[^[]*)*|(?R))*)\[/quote\]%x'
Hope it helps. Cheers
Upvotes: 0