RE: [PHP] preg_replace and emails
| From: | SLanger at spirit21 dot de | Date: | Mon, 05 May 2003 07:49:42 +0000 |
| Subject: | RE: [PHP] preg_replace and emails | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-146409@lists.php.net to get a copy of this message | ||
>http://www.php.net/manual/en/pcre.pattern.syntax.php
>
>I read the above page and supposely if I add (?<!foo)bar then it will
find
>an occurance of "bar" that is not preceeded by "foo" but it's not
>working
>for me.
>
>What am I doing wrong?
>$text =
>preg_replace("/(?<!mailto:)([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-
>Z]{2,3}|[0-9]{1,3})(\]?))/i","<A
>HREF=\"mailto:$1\">$1</A>", $text);
My guess is that you are not escaping the special php string characters.
Since you use a string to pass your regex you have to escape all
occurences of \, $ and " with a \ in front. The above should read
/(?<!mailto:)([\\w-?&;#~=}\\.\\/]+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-
>Z]{2,3}|[0-9]{1,3})(\\]?))/i (I was unable to test this at work :( )
You can test your regex by echoing to screen before passing it to
preg_replace that way you can see what is actually passed to preg_replace.
Don't forget php will first parse your string before the regex engine gets
a pass at it.
Hope this helps
Stefan Langer