On Fri, 18 Jun 2004 08:57:19 +0200 (CEST), Ulrik S. Kofod
<[EMAIL PROTECTED]> wrote:
> 
> Sorry to post this again but it's a little urgent.
> 
> The preg_replace in my script allocates a little memory every time it is called and
> doesn't free it again untill the script ends.
> 
> I don't know if it is normal behaviour for preg_replace or if it is my reg. exp.
> that causes this.
> 
> The problem is that I call preg_replace a little more than 30000 times and that
> causes quite a lot of memory to be allocated.
> >
> > $replace = "/^(([a-z]+?[^a-z]+?){".($count)."})(".$typedmask.")(.*)/iS";
> > $with = "$1<error-start sourcetext=".$corr['sourcetext']." id=".$corr['id']."
> > group=\"".$corr['grupper']."\" class=\"".$corr['ordklasse']."\"
> > corrected-from=\"".$corr['typed']."\"
> > corrected-to=\"".$corr['corrected']."\">$3<error-end
> > sourcetext=".$corr['sourcetext']." id=".$corr['id'].">$4";
> > $text = preg_replace ($replace,$with,$text,1);
> >
> > The problem is that it accumulates memory up to 200MB, that isn't freed again until
> > the script completes?

The S modifier that you're using means that it's storing the studied
expression. If the regexp changes each time around the loop then over
30000 iterations, that'll add up. See if removing that modifier helps
at all.

  $replace = "/^(([a-z]+?[^a-z]+?){".($count)."})(".$typedmask.")(.*)/i";

If that's not it, then these *might* save you some memory, although
I've not tested them:

I'm not entirely sure why you're matching (.*) at the end then putting
it back in with your replacement text. Without running it, I'd have
thought that you could leave out the (.*) from your pattern and the $4
from your replacement and get exactly the same effect.

  $replace = "/^(([a-z]+?[^a-z]+?){".($count)."})(".$typedmask.")/i";
  ...
  sourcetext=".$corr['sourcetext']." id=".$corr['id'].">";

You could use a non-capturing subpattern for $2 which you're not using
in your replacement.

  $replace = "/^((?:[a-z]+?[^a-z]+?){".($count)."})(".$typedmask.")/i";

And maybe a look-behind assertion for the first subpattern rather than
a capturing pattern then re-inserting $1.

  $replace = "/^(?<=(?:[a-z]+?[^a-z]+?){".($count)."})(".$typedmask.")/i";
  $with = "<error-start sourcetext=".$corr['sourcetext']." id=".$corr['id']."
  ...

   -robin

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to