I am attempting to do a URL to Link algorithm, however I need it to add target="_blank" at the end of the link, BUT I would also like it to be smart, as there are some where they will make their own links, but i want it so the ones that arent are done automatically, but ignoring the other links..
I have this so far:
function url2l($url) {
if (preg_match("/^http:/i", $url)) {
$str = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" target=\"_blank\">\\0</a>", $str);
} else {
echo "no";
}
return $str;
}
in theory it is supposed to look for http: in the beginning of a word.
Of course there are a multitude of issues with this (beyond it not working).
So any help would be apreciated. I can NEVER get this regex stuff.
edit::
I have done this (which seems to be working) but I am looking forward to any better ways...
function url2link($blank) {
if (!preg_match("/^<a href/i",$blank)) {
$blank = preg_replace( "/(?<!<a href=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\">$blank</a>", $blank );
}
return $blank;
}
Regards,