RE: [PHP] ereg_replace()
| From: | Lars Torben Wilson | Date: | Thu, 07 Sep 2000 03:34:01 +0000 |
| Subject: | RE: [PHP] ereg_replace() | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-15651@lists.php.net to get a copy of this message | ||
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);
}
> argument to filter() could hold the value of "L" to be returned is if
> ereg_replace() passed it as the second parenthesised token. The script is
> meant to replace the text between [L and ] with a corresponding
> value. This is why it is supposed to return "L" with 'return $flags;'.
>
> --Drew Vogel
>
> On Thu, 7 Sep 2000, WILSON, Nick wrote:
>
> > It's not a bug, it's the way you are using ereg_replace. The second
> > argument is the replacement text (or reg exp). So in your case, the
> > filter function is first called with arguments \1 and \2 (the first
> > \ will be an escape character).
> >
> > To do what you want to do, you will have to assign the matched text
> > to variables and then call filter, ie something like
> >
> > $templateLine = eregi_replace( $matchRegex, "\\1:\\2", $templateLine );
> > list($phase, $flag) = split (":", $templateLine);
> > filter("phase", "$flag" );
Hope this helps,
Torben
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+