Regex used to select and replace URLs in plain text with clickable links

(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#~\@!]*(\?\S+)?)?)?)

PHP

$regex = '(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#~\@!]*(\?\S+)?)?)?)';
$text = mb_ereg_replace($regex, '<a href="\\1">\\1</a>', $text);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.