Linkify: turning URLs into clickable links in PHP

by Arnold Daniels on 03/26/2012

Turning links like www.example.com and http://twitter.com 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:
TwitterLinkedIn

There are 10 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?

    ReplyReply
  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){
    if(empty($replace[$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.

    ReplyReply
  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?

    ReplyReply
  4. 8 May 2012Arnold Daniels says:

    @rick: Thanks

    ReplyReply
  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!

    ReplyReply
  6. 19 June 2012Techno radio says:

    To linkify, I’m using HTML Purifier with the linkify directive : http://htmlpurifier.org/live/configdoc/plain.html#AutoFormat.Linkify
    As an “added bonus” of using HTML Purifier, HTML code is protected against bad stuff like XSS.

    ReplyReply
  7. 7 October 2013ellenlangecouponcode.wordpress.com says:

    Hello, yeah this piece of writing is genuinely good
    and I have learned lot of things from it concerning blogging.
    thanks.

    ReplyReply
  8. 17 October 2013Fifa 14 Hack Android/iOS says:

    fantastic publish, very informative. I wonder why the opposite experts of this sector don’t notice this.
    You should continue your writing. I am sure, you have a great readers’ base already!

    ReplyReply
  9. 18 October 2013Https://Sites.Google.Com/Site/Benchdiscountcode says:

    Magnificent beat ! I would like to apprentice whilst you amend your web site, how can i subscribe for
    a blog site? The account helped me a appropriate deal.
    I were a little bit familiar of this your broadcast offered vivid transparent idea

    ReplyReply
  10. 25 October 2013web site says:

    Thanks for sharing your thoughts on Russ Pike Locksmith Directory.
    Regards

    ReplyReply

Write a comment:


+ seven = 14