Re: Help with preg_replace

From: Date: Mon, 19 Jan 2004 17:33:58 +0000
Subject: Re: Help with preg_replace
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-175049@lists.php.net to get a copy of this message
On Mon, Jan 19, 2004 at 07:15:53PM +0200, John Clegg wrote: > Hi Joel, > > Thanks for the reply. > > I have tried using "/" to delimit the string and I get the following error > > String > > $url = > "/http://www.foo.com/maxbid/Unsubscribe.php?EmailAddress=[MAIL]/"; > > Gives the error: > *Warning*: Unknown modifier '/' in* /test.php* on line *8* > > I found out that I could use ' to delimit from Example 5 in the php > manual page for preg_replace. Whoops, you're totally right. The single quote should work fine. I shouldn't underestimate PHP ;) > Also I need to replace only one occurance of the string at a time, so I > can't use str_replace :-( . > > Any other suggestions?? The problem is that there are still special chars that need escaping. Specifically, the question mark (?) and the square braces ([]). Here is some modified code (apologies for poor word-wrapping): <?php $code = "blah http://www.foo.com/maxbid/Unsubscribe.php?EmailAddress=[MAIL] blah "; $url = "'http://www.foo.com/maxbid/Unsubscribe.php\?EmailAddress=\[MAIL\]'"; echo "BEFORE : $code\n\n"; $replace = "Foo"; $code = preg_replace($url,$replace,$code,1); echo "AFTER: $code\n"; ?> When I run this, the output is: BEFORE : blah http://www.foo.com/maxbid/Unsubscribe.php?EmailAddress=[MAIL] blah AFTER: blah Foo blah Next time, I'll test out my answers before I give 'em. HTH! -- [ joel boonstra | gospelcom.net ]

« previous php.general (#175049) next »