How to replace a url with a link to it with regular expressions

Tagged:  

If you want to replace all the url's in a text with a link to them you should do something like this:
$text = preg_replace'@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);