I have a php function that changes urls to links. But I have problem when I insert for example pictures in format <img src="http://somelink.com/picture.jpg">
it also changes the inside url to link. How should I prevent this?
<?php
$string='http://www.somelink.com';
echo makelink($string);
function makeLink($string){
$string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i","$1http:$2",$string);
$string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</A>",$string);
$string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$string);
return $string;
}
?>
So, for exaple when I have input like this:
<img src="http://somelink.com/img.jpg"> http://somelink.com
I need the output to look like this:
<img src="http://somelink.com/img.jpg"> <a href="http://somelink.com" target="_blank">http://somelink.com</a>
What my code do now is:
<img src="<a href="http://somelink.com/img.jpg" target="_blank">http://somelink.com/img.jpg</a>"> <a href="http://somelink.com" target="_blank">http://somelink.com</a>
I hope you see the problem