views:

222

answers:

3

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 '~[Ą]~u' => 'A', '~[Ć]~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

+2  A: 

Have you tried iconv instead of handcrafting your replacement map?

Adapting the example from PHP manual

$text = "A String with lots of weird chars, like ĄĆ";

echo 'Original : ', $text, PHP_EOL;
echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text), PHP_EOL;
echo 'IGNORE   : ', iconv("UTF-8", "ISO-8859-1//IGNORE", $text), PHP_EOL;
echo 'Plain    : ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL;

would yield

Original : A String with lots of weird chars, like AC
TRANSLIT : A String with lots of weird chars, like AC
IGNORE   : A String with lots of weird chars, like AC
Plain    : A String with lots of weird chars, like AC

But this does not necessarily would work with any chars you throw in. Give it a try with your desired keys.

Gordon
That would be a good idea, but unfortunately for some countries I need to have full control over which character is translated to which, so I can't use an external tool for that task.
Silvio Donnini
A: 

i had same problem (memory leak) not with preg_replace and i "heard" that probably its a bug in php 5.2

I am not sure but if you try the same script with php 5.0 might work.

Just for the record.

ntan
If this a known bug in PHP, you'll find it at http://bugs.php.net/
Gordon
Yeah, can you add a link here that leads to the bug?
Till
A: 

im expecting this issue

Clal