Memory leak with preg_replace
- by Silvio Donnini
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