Re: more regex fun

From: Date: Fri, 02 Jun 2000 16:02:46 +0000
Subject: Re: more regex fun
Groups: php.general 
Request: Send a blank email to php-general+get-909@lists.php.net to get a copy of this message
> I got this regex code working (thanks to Andreas and Zak) last night: > > $urlvars = $nav->urlvars(); > $ret = preg_replace( "/<B>(.*?)<\/B>/ei", > "\"<A > > href=$searchpageurl?$urlvars&qu=\".urlencode('\\1').\"&hi=20&ty=any\".\"><B>\".'\\1'.\"</B></A>\"", > > $text ); > > > For description and example of wht it does, please see thread from last > night (started with "PHP function inside a regex - possible?" post). > > Anyway, my problem now is that when there is an apostrophe or double > quote in \\1, I get a 'Failed evaluating code' error. I tried changing > qu=\".urlencode('\\1') to qu=\".urlencode(addslashes('\\1')), but > this > made no difference. > Any ideas how I guard against single and double quotes? You're out of luck here. With current implementation, there are basically two ways to do it: 1. Use preg_match_all() to grab all your <b>..</b> chunks and then process them and use preg_replace() to replace them back in. 2. Use preg_replace() to find ' and " inside <b>..</b> chunks and escape them with \, then use another preg_replace() for the final result. Like I said, I plan to add something like /f modifier to preg_replace() so that you can just provide a name of a function that will be passed the contents of each match and the function will return the replacement string. Then you can do whatever you want. -Andrei Windows 2000 is certified not to crash more than once a day, so what is the bootup time, 24 hours? -- Sam Liddicott

« previous php.general (#909) next »