This is the regular expression I'm currently using:
$url = '#(^|[\n (])([\w]+?://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])#is';
It does have the unfortunate side effect of including the trailing ) as part of the URL. I'm not sure what the right solution is, given that parenthesis in URLs are valid and legal. How can the system be programmed to know whether you intended that character to be a part of the URL, or rather the end of a sentence?