Re: PHP function inside a regex - possible?

From: Date: Thu, 01 Jun 2000 19:14:12 +0000
Subject: Re: PHP function inside a regex - possible?
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-862@lists.php.net to get a copy of this message
Thanks Zak, unfortunately the /g modifier doesn't appear to be supported and with the U modifier I get nothing where term2 should be bolded. I have sent a file with some 'real-life' sample text to Andrea, he is going to look at it, his last suggestion worked for him but not for me, he's using a PHP4 snapshot from late March and using PHP4B3. I'm going to build a PHP4RC1 binary now... - Kev Zak Greant wrote: > Hi Kev, > > Try putting a g after the /e in your regex. Perl regular expressions stop > at newlines by default - the g modifier makes the regex consider newlines > to be just another character. You might also need to make the expression > non-greedy to keep it from eating up everything between the first <b> and > the last </b> in the text. If this is the case add a U after the g. > > Good Luck! > > Zak > > At 06:47 PM 6/1/00 +0000, kev wrote: > >Aaarrrrgh!! Don't you always get the most problems when it's urgent?! > > > >Running a substantial block of text through this (about 2000+ characters) > >doesn't seem to make any change to the text at all. > > > >So, my question now, is: how can I modify the regex to modify every bolded > >term > >throughout the entire text, and is there a size limit on a string being put > >through a regex? If there is, how can I get round it without having to > >physically split it up into sections, which will be very messy as I would have > >to ensure that it wasn't split in the middle of <B> or </B> tag? > > > >thanks, > > > >- Kev > > > > > >kev wrote: > > > > > This isn't giving the desired results though when there is more than > > one term > > > to be modified, eg with this string: > > > > > > $text = "Turn <B>term 1</B> and <B>term 2</B> to a > > > link"; > > > > > > It turns everything in between the first <B> and the last </B> into a > > > link! > > > > > > How do I guard against that, but ensure that it still modifies every bolded > > > term? > > > > > > thanks, > > > > > > - Kev > > > > > > Andreas Pour wrote: > > > > > > > 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 > > > > > > > > -- > > > > 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 > > > > > > -- > > > 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 > > > > > >-- > >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 > > -- > 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 (#862) next »