-4

I was arguing with many regexp to find one supporting simply www.*.com url without the need to put http:// before.

At present I'm using:

  $text= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" >$3</a>", $text);
$text= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" >$3</a>", $text);
$text= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\">$2@$3</a>", $text);

but it's not sufficient, any ideas?

M4rk
  • 2,172
  • 5
  • 36
  • 70

1 Answers1

0

I suggest you to use a naive regex and then the FILTER_VALIDATE_URL filter. example:

$text = preg_replace_callback('~(?:(\b(?:ht|f)tps?://)|\bwww\.)[^\s"\')(,]+~i',
    function ($m) { 
        $url = ($m[1]? '':'http://') . $m[0];
        return (filter_var($url, FILTER_VALIDATE_URL)) ?
            '<a href="' . $url . '">' . $m[0] . '</a>' : $m[0]; },
    $text);
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125