Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum length for a string for preg_replace() in PHP?

Tags:

regex

php

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?

like image 845
Leo Jiang Avatar asked Aug 02 '12 03:08

Leo Jiang


1 Answers

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();
    }
like image 57
Jofator Avatar answered Oct 05 '22 04:10

Jofator