RE: [PHP] ereg_replace()

From: Date: Thu, 07 Sep 2000 03:44:18 +0000
Subject: RE: [PHP] ereg_replace()
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-15656@lists.php.net to get a copy of this message
Erf. Sorry to follow up to myself--I think I got the order of arguments you intended reversed (i.e. \2 and $flags should be be \1 etc), but the gist of the text, I think, is correct. Sorry, Torben Lars Torben Wilson writes: > > I believe Nick is correct. > > Drew P. Vogel writes: > > If the filter() function is being called first, with the arguments > > interprected first, then how does it return "L"? The only way the $flags > > It's not returning "L". It's returning "\2". This then appears as > the > second argument to eregi_replace() (as a string); it is not until after > this has occurred that eregi_replace() parses the \2 into an 'L'. Like > this: > > Step 1: > $templateLine = eregi_replace($matchRegex, filter("\\2", "\\1"), > $templateLine); > > At this point, filter() is called first, as noted > before. Therefore, the arguments to filter() are "\\2" and "\\1", > since the reference replacement hasn't occurred yet. This is why > echoing $flags in filter() gives '\2'. > > Step 2: > filter() has returned the value of $flags, '\2', leaving the line > above looking like this to the parser: > > $templateLine = eregi_replace($matchRegex, '\2', $templateLine); > > eregi_replace() now executes, and the '\2' is replaced by the second > parenthesized substring of the match: 'L'. > > ...and there you go. It may be a good idea to rework the logic to > avoid these little subtleties; Nick gave one example; this would be > another: > > while (eregi($matchRegex, $templateLine, $matches)) { > $replace_text = filter($matches[2], $matches[1]); > $templateLine = eregi_replace($matchRegex, $replace_text, $templateLine); > } -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#15656) next »