note 98843 added to function.preg-replace
| From: | AmigoJack at osu1 dot php dot net | Date: | Sat, 10 Jul 2010 13:13:54 +0000 |
| Subject: | note 98843 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170862@lists.php.net to get a copy of this message | ||
If you're using preg_replace() on huge strings you have to be aware of PREG's limitations.
In fact, after each preg_xxx() function you should check if PREG internally failed (and by
"failure" I don't mean regexp syntax errors).
On default PHP installations you will run into problems when using preg_xxx() functions on strings
with a length of more than 100'000 characters. To workaround rare occasions you can use this:
<?php
$iSet= 0; // Count how many times we increase the limit
while( $iSet< 10 ) { // If the default limit is 100'000 characters the highest new limit
will be 250'000 characters
$sNewText= preg_replace( $sRegExpPattern, $sRegExpReplacement, $sVeryLongText ); // Try to use
PREG
if( preg_last_error()== PREG_BACKTRACK_LIMIT_ERROR ) { // Only check on backtrack limit failure
ini_set( 'pcre.backtrack_limit', (int)ini_get( 'pcre.backtrack_limit' )+
15000 ); // Get current limit and increase
$iSet++; // Do not overkill the server
} else { // No fail
$sVeryLongText= $sNewText; // On failure $sNewText would be NULL
break; // Exit loop
}
}
?>
However, be careful: 1.) ini_set() may be forbidden on your server; 2.) preg_last_error() doest not
exist prior to PHP 5.2.0; 3.) setting a backtrack limit too high may crash PHP (not only the script
currently executed). So if you work a lot with long strings you definitly have to look out for a
real solution!
See also natedubya's comment (02-Oct-2009 04:08).
----
Server IP: 85.214.154.209
Probable Submitter: 85.180.154.82
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/98843
Del: integrated -- https://master.php.net/note/delete/98843/integrated
Del: useless -- https://master.php.net/note/delete/98843/useless
Del: bad code -- https://master.php.net/note/delete/98843/bad+code
Del: spam -- https://master.php.net/note/delete/98843/spam
Del: non-english -- https://master.php.net/note/delete/98843/non-english
Del: in docs -- https://master.php.net/note/delete/98843/in+docs
Del: other reasons-- https://master.php.net/note/delete/98843
Reject -- https://master.php.net/note/reject/98843
Search -- https://master.php.net/manage/user-notes.php