There are many similar questions, but I've tried about 15 different preg_match examples and none are working fully.
I have a lot of user submitted content and much of it has urls.. sometimes in the form http://www.site.com/page and sometimes like www.site.com and quite often contained in parentheses (www.site.com/page.html).
I have had no luck finding a pattern that will parse a string and convert all those to absolute html links. Wondering if anyone can help me. I found a few regex finding expressions that seemed like they would work, but I do not know how to properly convert to absolute html link when some are with the http and some without...
Here are a few expressions that I have tried:
function makeLinks($text) {
$text = preg_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'(<a href="\\1">\\1</a>)', $text);
$text = preg_replace('(www\.[a-zA-Z0-9\-]\.[^ ]+)',
'(<a href="\\1">\\1</a>)', $text);
return $text;
}
function makeLinks($text) {
$text = preg_replace('~(?:www|http://)\S+~', '<a href="$0">$0</a>', $text);
return $text;
}
function makeLinks($text) {
$text = preg_replace( '@(?<![.*">])\b(?:(?:https?|ftp|file)://|[a-z]\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]@i', '<a href="\0" target="_blank">\0</a>', $text );
return $text;
}