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 http://fail.org
amet http://abc.de.fg.com?bar=baz';
$rx = '%[^"](?P(?:https?://|www\.)(?:[-_a-z0-9]+\.)+(?:[a-z]{2,4}|museum/?)(?:[-_a-z0-9/]+)?(?:\?[-_a-z0-9+\%=&]+)?(?!'.$matches['link'].'';
}, $string).PHP_EOL;
the output string is
lorem ipsumwww.foo.bar dolor sit http://fail.org
amethttp://abc.de.fg.com?bar=baz
The regex should work as intendet, an example string of yours could help