Here's the idea:
With regular expressions.
$txt = preg_replace("@(|\s)([:\s\"]+)://([\s]+)@","$1<a href=\"$2://$3\" target=\"_blank\">$2://$3</a>",$txt); $txt = preg_replace("@<a href=\"([:/\"\s])://([\"])\"([>])>([<])</a>@", "<a href=\"$1://$2\" target=\"_blank\">$4</a>",$txt);