Re: Help with preg_replace
| From: | John Nichel | Date: | Mon, 19 Jan 2004 17:13:54 +0000 |
| Subject: | Re: Help with preg_replace | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-175040@lists.php.net to get a copy of this message | ||
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 **********************$url = "/http:\/\/www.foo.com\/maxbid\/Unsubscribe.php?EmailAddress=\[MAIL\]/"; Will replace http://www.foo.com/Unsubscribe.php?EmailAddress=[MAIL] only. If you're looking to replace any URL, you need a regular expression. The first argument to preg_replace is the pattern you're looking for, so you need to enclose it in slashes ( /pattern/ ), and escape out any special characters. -- By-Tor.com It's all about the Rush http://www.by-tor.com