Linkify: turning URLs into clickable links in PHP

by Arnold Daniels on 03/26/2012

Turning links like and into clickable links. Sounds like an easy task, right? We’ll there are a few problems that might arise, especially if the text is already HTML formatted.

This function first takes out all potential dangers, by extracting links and tags and replacing them with a placeholder. It than extracts all URLs and replaces them with a placeholder, storing the full HTML link. At the end it replaces all placeholders with the links and tags.

This is also a nice example for how to use PHP closures.

Arnold Daniels

I've spend a big part of my life behind a computer, learning about databases (MySQL), programming (PHP) and system administration (Linux). Currently I playing with HTML5, jquery and node.js.

More Posts

Follow Me:

There are 7 comments in this article:

  1. 27 March 2012Andy says:

    god that’s one hell of a regex explosion for something seemingly so simple

    wouldn’t using a filter_var() function minimise the regex?

  2. 27 March 2012Brian Moon says:

    I promise I am here to educate not hate. =)

    You use preg_replace_callback very carelessly here. Closures in other languages are near free. In PHP, furnction overhead is a real thing. You could have used preg_match_all() and done the foreach after it and achieved the same outcome without the function overhead of a closure. Additionally, you could have used str_replace() with arrays to put the original anchor tags back in place and avoided both a regular expression compilation and a closure. Now, if the code above was more readable, it may be worth the 2ms (for one call and just for the tag removal and replacement, not the switch) it will cost you. But, it is not. Functions in PHP should only be used for code reuse. Never for code organization if you care at all about performance. The below code runs about twice as fast as your first and last preg_replace_callback uses.

    // take out the tags
    preg_match_all('~(.*?|)~i', $value, $matches);

    foreach($matches[1] as $k => $v){
    $replace[$v] = "";
    $search[] = $v;

    $value = str_replace($search, $replace, $value);

    // put them back
    $value = str_replace($replace, $search, $value);

    Also, would match and I am not sure if you intended that or not. would seem more appropriate.

  3. 28 March 2012Arnold Daniels says:

    @Brian It would be interesting to see how these solutions compare. Could you write a function that does the same as above using preg_match and str_replace?

  4. 8 May 2012Arnold Daniels says:

    @rick: Thanks

  5. 27 May 2012marathon running shoe says:

    Very great post. I simply stumbled upon your weblog and
    wanted to mention that I have truly loved browsing
    your blog posts. In any case I will be subscribing in your
    rss feed and I hope you write again very soon!

  6. 19 June 2012Techno radio says:

    To linkify, I’m using HTML Purifier with the linkify directive :
    As an “added bonus” of using HTML Purifier, HTML code is protected against bad stuff like XSS.

  7. 24 March 2015DC says:

    That’s what I’ve been looking for (y) Thank you!


Write a comment: