On Mon, Jan 19, 2004 at 06:50:37PM +0200, John Clegg wrote: > Hi > > I am having some problems with preg_replace and I wondering if someone > could suggest a fix or explain why it is not working. > > > > Here is the Code: > ********************* > <?php > $code = "blah http://www.foo.com/Unsubscribe.php?EmailAddress=[MAIL] blah"; > $url = "'http://www.foo.com/maxbid/Unsubscribe.php?EmailAddress=[MAIL]'"; > echo "BEFORE : $code<br><br>"; > $replace = "Foo"; > $code = preg_replace($url,$replace,$code,1); > echo "AFTER: $code<br>"; > ?> > ********************** > > Here is the results of the script: > > ********************** > BEFORE : blah > http://www.qxlmaxbid.com/maxbid/Unsubscribe.php?EmailAddress=[MAIL] blah > > AFTER: blah > http://www.qxlmaxbid.com/maxbid/Unsubscribe.php?EmailAddress=[MAIL] blah > > ********************** > > It should give the following result: > > ********************** > > AFTER: blah Foo blah > > **********************
preg_replace expects the first argument to be a pattern, which should be enclosed in forward slashes ("//"). So your $url variable should be surrounded with slashes. You have single quotes in your $url variable, but I don't think that PHP behaves like perl in letting you use whichever delimiter you choose. Try replacing the single quotes with forward slashes. Additionally, if you're only doing a simple string substitution, you should use str_replace instead: http://php.net/str_replace You won't need the "/" delimiters, and it will be faster than using preg_replace. joel -- [ joel boonstra | gospelcom.net ] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php