Re: regex backreference help needed (PCRE)

From: Date: Thu, 01 Jun 2000 17:56:00 +0000
Subject: Re: regex backreference help needed (PCRE)
Groups: php.general 
Request: Send a blank email to php-general+get-858@lists.php.net to get a copy of this message
> 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

« previous php.general (#858) next »