Re: regex backreference help needed (PCRE)
| From: | kev | Date: | Thu, 01 Jun 2000 19:10:14 +0000 |
| Subject: | Re: regex backreference help needed (PCRE) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-861@lists.php.net to get a copy of this message | ||
Thanks Andrei,
I have got this far now.
Please could you follow the other thread, I'm having other problems now!
thanks,
- Kev
Andrei Zmievski wrote:
> > I'm trying to use backreferences in the PCRE stuff, but this code:
> >
> > <?
> > $text = "Turn <B>bold</B> to a link";
> >
> > $newtext = preg_replace( "/<B>(.*)?<\/B>/e",
> > "<A href=\\1><B>\\1</B></A>",
> > $text );
> >
> > echo "\$text = $text<BR>";
> > echo "\$newtext = $newtext<BR>";
> > ?>
> >
> > ... produces this odd error:
> >
> > Parse error: parse error in
> > /usr/local/apache/htdocs/megsoc2/test/boldlinks1.php on line 0
> >
> > and all the regex has done is inserted a space instead of the
> > backreference.
> >
> > Any ideas?
>
> Yes. You're using /e modifier which expects the replacement string to be
> a valid PHP expression once the backreferences get substituted. From
> your previous message, I can see that you want to urlencode something
> too. A way to achieve this is to do this:
>
> $newtext = preg_replace("/<B>(.*)?<\/B>/e",
> "'<A
> href='.urlencode('\\1').'><B>\\1</B></A>",
> $text );
>
> I plan to add another modifier that will let you call a function of your
> choice with the captured substrings so that you can do appropriate
> processing and return replacement result instead of fussing around with
> /e.
>
> -Andrei
>
> "In My Egotistical Opinion, most people's C programs should be indented
> six feet downward and covered with dirt." -- Blair P. Houghton
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net