Re: PHP function inside a regex - possible?

From: Date: Thu, 01 Jun 2000 18:10:58 +0000
Subject: Re: PHP function inside a regex - possible?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-845@lists.php.net to get a copy of this message
Hi, Kevin, As to your example, you have to have the replacement string be suitable for an "echo" statement, I think. The following works, anyway: $newtext = preg_replace("/(<B>(.*)<\/B>)/e", "\"<A href=\" . '\\2' . \"><B>\" . '\\2' . \"</B></A>\"", $text ); Good luck, Andreas kev wrote: > > Zak McGregor wrote: > > > In php 3.x, no. In php 4.x try: > > preg_replace("/<B>(.*)?<\/B>/", "<A > > > > HREF=\"whatever.php3?search=".urlencode('\\1')."\">".'\\1'."<\/A>"); > > > > I'm using PHP4. > I don't understand how that could work, how will it know what '\\1' is > in the urlencode() ? The function has not even been entered at that > point, so surely urlencode('\\1') will just equate literally to '\\1' > urlencode()ed. > > Anyway, I tried this little example as a starting point to build up to > the full thing eventually: > > $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>"; > > Please try this example, and you'll see it doesn't work. Can you see > what's wrong with it? > Also, is question mark (?) needed? > In Perl, I use $1 for a back-reference, is \\1 the PCRE equivalent? > > thanks, > > - Kev > > -- > 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

« previous php.general (#845) next »