Home » Web Development » PHP/mySQL » PHP Regex to Make Twitter Links Clickable
PHP Regex to Make Twitter Links Clickable

PHP Regex to Make Twitter Links Clickable

This is just a quicky post, not one of my usual long, rambling diatribes. This week is madness, even by my own absurd standards, but I didn’t want to miss jotting this down in case it might be helpful to others.

I’ve had varying degrees of success trying to find a series of preg_replace statements that would correctly replace output generated by Twitter’s RSS feeds (which do not contain any linking HTML) to autolink hyperlinks, @replies and hashtags, so I finally sat down and sorted it out myself.

The code below should correctly autolink all of the autolinkables in your PHP script:

  • links @username to the user’s Twitter profile page
  • links regular links to wherever they should link to
  • links hashtags to a Twitter search on that hashtag
function twitterify($ret) {
  $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2", $ret);
  $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2", $ret);
  $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
  $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
return $ret;
}

Someday I’ll try to find the time to write a regex primer/tutorial. Regex is another one of the things, like SVN, that seems scary and incomprehensible to many people, but eventually it clicks and makes sense. In the meantime, if you’re interested in learning more about using Regex in PHP, check out the following resources:

More Regexy Goodness:

Make sure you leave yourself some time to actually try out some examples, and dissect the examples they give so that you really grok what’s happening. Once it clicks, it seems so simple, you won’t believe you ever let it beat you up and take your lunch money.

Image by xkcd, via their awesome web store. The comic rocks my geeky face off. I buy my clothes there. So should you.

PS – still really, really hate posting code in WordPress. Even in HTML mode, it keeps converting my fscking special characters, which then get double/triple/etc converted.

Advertisement

Site5

Advertisement

Net Tuts

flattr this!

About snipe

I’m a tech geek/dev/infosec-nerd/scuba diver/blacksmith/sword-fighter/crime fighter/ENTP/warcrafter/activist, and the former CTO and CSO at a business innovation agency in New York City. Tweet at me @snipeyhead or read more...
  • http://twitter.com/cuneytgurcan Cuneyt Gurcan Akcora

    I want you to know that i LOVE you! Finally I found something that works!!!

  • http://www.snipe.net snipe

    Yes, of course. Smart quotes are not recognized as quote marks.

  • Deratrius

    Nice work, noticed that if there is a regular link between parentheses the the link isn’t parsed.
    eg: http://www.link.com is parsed but (www.link.com) isn’t.

  • Nic

    Thanks, this is what i’m looking for.

  • http://twitter.com/qu4cks0ft Airlangga bayu s.

    Thanks, function work on my code

  • Lucian

    Awesome! Thank you so much for writing this! It’s perfect!

  • Blocki

    Thanks man! This really helped me a lot, I’m gonna use it in my own twitter app on facebook!

  • http://enjoyallthings.com/ Angela Geise

    Thanks so much for this! I’ve been working on a script for collecting tweets and I was missing functionality to get Twitter handles and hash tags clickable to where they need to go. This helps a ton!

  • Benny

    This helped a lot! Thanks!

  • Biketrails.nl

    Great stuff, really helped me out recreating my timeline using the twitter API and php/mysql.

    Check it out: http://ww.biketrails.nl

  • http://twitter.com/DOADarkAngel Darkest of Angels

    Very nice, tyvm

  • http://psdtomagento.net psd to magento
  • http://twitter.com/iamjeff75 jeff

    Hi, thanks a lot for this code snippet.
    It seems that the search subdomain for hashtags is not needed anymore, I had to remove the subdomain in the regexp to fix the link.

  • Ter

    Hi…this is great code and an easy simple solution to making entities click-able in a twitter plugin. My only concern is when tweets end in with urls it seems to break the url. Don’t know if anyone else experiences this in their plug-ins but it is certainly a problem in mine.

    • Aron

      Yes I’ve just implemented this and noticed that too, I guess I will have to look at an alternative unless the author can update it!

  • Stephan Fischer

    I use this one:

    function twitterify($ret)

    {

    $ret = preg_replace(“#(^|[n ])([w]+?://[w]+[^ "nrt< ]*)#", "\1\2“, $ret);

    $ret = preg_replace(“#(^|[n ])((www|ftp).[^ "tnr< ]*)#", "\1\2“, $ret);

    $ret = preg_replace(“/@(w+)/”, “@\1“, $ret);

    $ret = preg_replace(“/#(w+)/”, “#\1“, $ret);

    return $ret;

    }

  • Baljka Gan

    Just Awesome!

  • Jame

    OK, this is good but old and doesn’t always get round those edge cases. I spent quite a bit of time looking for the definitive answer to this and behold it comes from twitter themselves. *phew* https://dev.twitter.com/docs/tco-url-wrapper/how-twitter-wrap-urls

  • Phil Kershaw

    I was looking into this for a client and you don’t need Regex at all to make the links in a twitter feed clickable. If you want to know more check my blog post about it and let me know what you think: http://blog.philkershaw.me/2013/11/clickable-links-in-your-twitter-feed.html