Re: Help with preg_replace
| From: | John W. Holmes | Date: | Mon, 19 Jan 2004 18:21:05 +0000 |
| Subject: | Re: Help with preg_replace | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-175066@lists.php.net to get a copy of this message | ||
joel boonstra wrote:
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.In $url, the periods still need to be escaped, too. This is where preg_quote() comes in handy. $url = "http://www.foo.com/maxbid/Unsubscribe.php?EmailAddress=[MAIL]"; $url = preg_quote($url); $code = preg_replace("#$url#",$replace,$code,1); I hope the OP took notice of the str_replace() remark, too. :) -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com