1

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;
}
Damon
  • 10,493
  • 16
  • 86
  • 144

2 Answers2

2

I ended up using this string which seems to be performing well on all necessary cases:

function makeLinks($text) {
    $text = preg_replace('%(((f|ht){1}tp://)[-a-zA-^Z0-9@:\%_\+.~#?&//=]+)%i',
    '<a href="\\1">\\1</a>', $text);
    $text = preg_replace('%([[:space:]()[{}])(www.[-a-zA-Z0-9@:\%_\+.~#?&//=]+)%i',
    '\\1<a href="http://\\2">\\2</a>', $text);

        return $text;
}
Damon
  • 10,493
  • 16
  • 86
  • 144
  • You have to add ! and , in your regex and add [s]? in the first preg for https : `$text = preg_replace('%(((f|ht){1}tp[s]?://)[-a-zA-^Z0-9@:\%_\+.,!~#?&//=]+)%i','\\1', $text); $text = preg_replace('%([[:space:]()[{}])(www.[-a-zA-Z0-9@:\%_\+.,!~#?&//=]+)%i','\\1\\2', $text);` As there are , and ! in some URLs (like metronews.fr for example) – zeuf May 28 '14 at 11:03
  • And I had to add ) and ( in the regex too. – zeuf Jun 19 '14 at 13:54
0

This was a good start:

function makeLinks($text) {
    $text = preg_replace('~(?:www|http://)\S+~', '<a href="$0">$0</a>', $text);
    return $text;
}

$0 is the full match. If you group just the section without http:// or www. or http://www., you can concatenate that to the front.

Try this if you are still looking for an answer:

function makeLinks($text) {
    $text = preg_replace('~(?:http://|)(?:www\.|)(\S+)~', '<a href="http://www.$1">$0</a>', $text);
    return $text
}
RedSoxFan
  • 634
  • 3
  • 9
  • that didn't work for me... it turned every word into a hyperlink – Damon Mar 06 '11 at 02:46
  • 1
    Oh sorry try `~\b(?:http://www.|http://(?!www.)|(?<!http://)www.)(?:\.|)(\S+)\b~` – RedSoxFan Mar 07 '11 at 00:13
  • I am not getting expected result. Please use the proper variables inside functional flow. ex: ($tect, $text, $test) used to indicate single variable. – Navane Mar 14 '13 at 16:01
  • @Navane Oops sorry that is a typo. I am going to fix it for anyone looking at this page but there is already an accepted answer. – RedSoxFan Mar 23 '13 at 01:30