Linkify Regex Function PHP Daring Fireball Method

Go To


So, I know there are a ton of related questions on SO, but none of them are quite what I'm looking for. I'm trying to implement a PHP function that will convert text URLs from a user-generated post into links. I'm using the 'improved' Regex from Daring Fireball towards the bottom of the page: The function does not return anything, and I'm not sure why.

if ( false === function_exists('linkify') ):   
  function linkify($str) {
$pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';     
return preg_replace($pattern, "<a href=\"\\0\" rel=\"nofollow\" target=\"_blank\">\\0</a>", $str);      

Can someone please help me get this to work? Thanks!

2012-04-03 22:15
by Jeff
This exact question came up before, but it's indeed difficult to google. But enabled error_reporting would have told you instantly - mario 2012-04-03 22:29


Try this:

$pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))';     
return preg_replace("!$pattern!i", "<a href=\"\\0\" rel=\"nofollow\" target=\"_blank\">\\0</a>", $str); 

PHP's preg function do need delimiters. The i at the end makes it case-insensitive


If you use # as the delimiter, you wan't need to escape the ! in the pattern as such use the original pattern string (the pattern does not have a #): "#$pattern#i"

Update 2

To ensure that the links are correct, do this:

$pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';
return preg_replace_callback("#$pattern#i", function($matches) {
    $input = $matches[0];
    $url = preg_match('!^https?://!i', $input) ? $input : "http://$input";
    return '<a href="' . $url . '" rel="nofollow" target="_blank">' . "$input</a>";
}, $str); 

This will now append http:// to the urls so that browser doesn't think it is a relative link.

2012-04-03 22:18
by d_inevitable
Sorry, I have just updated my answer now - d_inevitable 2012-04-03 22:26
Thanks. Will try to avoid answers without explanation in the future - d_inevitable 2012-04-03 22:33
Thank you @d_inevitable ! It seems to be recognizing the links correctly now. Works perfectly for links starting with 'http:'. However for something like '' the new tab address shows 'http//' I'm a bit of a novice-- thanks so much - Jeff 2012-04-03 22:38
@Jeff this is because the new link must include the http:// prefix in the href attribute. I don't think you can do this with single function call. Try preg_replace_callback and an if-statement that will prepend http:// when necessary - d_inevitable 2012-04-03 22:40
Thanks again. I'll look into that and give it a shot - Jeff 2012-04-03 22:45
@Jeff ive udpated my answer accordingly - d_inevitable 2012-04-03 22:48
Wow, that's great! Seems to work for and but ends up as http://http// 2012-04-03 23:01
$url = preg_match('!^http?s://!i', $input) ? $input : "http://$input"; should be changed to $url = preg_match('!^https?://!i', $input) ? $input : "http://$input"; The question mark just had to be moved one spot over - Jeff 2012-04-03 23:36
this is working great. Wondering if you have any advice to make it xss safe using PHP. Thanks! - Jeff 2012-04-25 16:27


I was looking to just get the urls from a string using the same regex from the answer above by d_inevitable and wasn't looking to turn them into links or care about the rest of the string, I only wanted the urls with in the string so this is what I did. Hope it helps.

 * Returns the urls in an array from a string.
 * This dos NOT return the string, only the urls with-in.
function get_urls($str){

    $regex = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';
    preg_match_all("#$regex#i", $str, $matches);
    $urls = $matches[0];
    return $urls;

2014-09-08 02:30
by toystory