0

Possible Duplicate:
Replace URLs in text with HTML links

I'm passing the string variable which contains multiple urls, through function below to get same thing only with proper HTML links.

public function convertUrlsToLinks($text){
    return preg_replace( '@(?<![.*">])\b(?:(?:https?|ftp|file)://|[a-z]\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]@i', '<a href="\0" target="_blank">\0</a>', $text );
}

It doesn't work at all. What am I missing?

Code must skip existing links, <img>'s src values (or something like that.)

Community
  • 1
  • 1
heron
  • 3,611
  • 25
  • 80
  • 148

1 Answers1

0

assuming you already have an html document, I limited the recognition of URLs to

  • not start with an "
  • start with http or www

i came up with a solution like this:

$string = 'lorem ipsum www.foo.bar dolor sit <a href="http://fail.org">http://fail.org</a><img src="www.foo.bar"> amet http://abc.de.fg.com?bar=baz';
$rx = '%[^"](?P<link>(?:https?://|www\.)(?:[-_a-z0-9]+\.)+(?:[a-z]{2,4}|museum/?)(?:[-_a-z0-9/]+)?(?:\?[-_a-z0-9+\%=&]+)?(?!</a)(\W|$))%ui';
echo preg_replace_callback($rx, function($matches) {
    return '<a href="'.$matches['link'].'">'.$matches['link'].'</a>';
}, $string).PHP_EOL;

the output string is

lorem ipsum<a href="www.foo.bar ">www.foo.bar </a>dolor sit <a href="http://fail.org">http://fail.org</a><img src="www.foo.bar"> amet<a href="http://abc.de.fg.com?bar=baz">http://abc.de.fg.com?bar=baz</a>

The regex should work as intendet, an example string of yours could help

DesertEagle
  • 599
  • 5
  • 18