Memory leak with preg_replace

Posted by Silvio Donnini on Stack Overflow See other posts from Stack Overflow or by Silvio Donnini
Published on 2010-02-26T10:42:57Z Indexed on 2010/05/14 7:04 UTC
Read the original article Hit count: 355

Filed under:
|
|
|

I'm using the preg_replace function to replace accents in a string, I'm working with UTF-8.

I have incurred in what seems to be a memory leak, but I can't isolate the root cause, my code is rather simple:

    preg_replace(
        array_keys($aToNoAccents),
        array_values($aToNoAccents),
        $sText
    );

where $aToNoAccents is an associative array with entries like '~[A]~u' => 'A', '~[C]~u' => 'C',. My script prints this error for the above line:

Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 3039 bytes)

Obviously it's not a matter of increasing the allowed memory for PHP, (a 1Gb footprint is way off the scale of my application). Also, that line is executed thousands of times without problems but, just for some cases which are difficult to reproduce, it yields the error.

Is anyone aware of memory problems with preg_replace and UTF-8 strings? Am I to use special care in passing actual parameters to such function?

I'm using PHP 5.2.6-3 with Suhosin-Patch

thanks

Silvio

© Stack Overflow or respective owner

Related posts about php

Related posts about preg-replace