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