Re: preg_replace and eval
| From: | Erwin | Date: | Mon, 28 Oct 2002 11:31:45 +0000 |
| Subject: | Re: preg_replace and eval | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-121684@lists.php.net to get a copy of this message | ||
> The following line sort of works:
> $retVal =
> preg_replace("/(.*){parent.(.*)}(.*)/e","'\\1'.\$this->parentNode-
> >getProper tyValue('\\2').'\\3'",$retVal);
>
> However, if there are more than one string to replace it only works
> on the last one. So:
> caption = "the parent's coordinates are ({parent.left},
> {parent.top})" would only return something like: "the parent's
> coordinates are ({parent.left}, 100)"
That is because the first (.*) skips the first parent.x pattern. It seems
the string is parsed backwards.
To avoid this problem, be more specific in your regexp. Don't use (.*), but
([^{]*) instead. The regexp which works in your example is the following:
/([^{]*){parent.([^{]*)}([^{]*)/e
HTH
Erwin