PHP reg expr. replace ALL URLs except img src URLs

Posted by zilveer on Stack Overflow See other posts from Stack Overflow or by zilveer
Published on 2011-01-08T21:50:45Z Indexed on 2011/01/08 21:53 UTC
Read the original article Hit count: 121

Filed under:

Hi, I have searched but havent been able to find my answer. It follows like:

I would like to replace all URL in a string to links except the URLs within img src tag.

I have a regular expression for replacing all the URLs to links, but would like it to NOT replace the URLs within img src="" attribute.

How can i do this?

Here is the code for replacing all URLs:

/*** make sure there is an http:// on all URLs ***/ $str = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$str); /*** make all URLs links ***/ $str = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</a>",$str);

/Regards

© Stack Overflow or respective owner

Related posts about php