Extract URLs from text in PHP

Probably the safest way is using code snippets from WordPress. Download the latest one (currently 3.1.1) and see wp-includes/formatting.php. There’s a function named make_clickable which has plain text for param and returns formatted string. You can grab codes for extracting URLs. It’s pretty complex though.

This one line regex might be helpful.

preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match);

But this regex still can’t remove some malformed URLs (ex. http://google:ha.ckers.org ).

See also:
How to mimic StackOverflow Auto-Link Behavior

Leave a Comment