Twitter status text links made clickable

23rd February 2010 by Jason C. Filed under: Code-bits

A new design agency being formed by an existing client needed a site. They had a good design ready, I just had to build the thing. It’s built in Wordpress, but I also pull in their Twitter stream on the client side.

Twitter’s api is easy enough to use. I opted for the JSON-p format, but something I didn’t realise at the time was that the status updates are purely the plain text. So any URLs, usernames or hashtags are just plain text: no wrapping in <a></a> tags. It’s an easy problem to solve, though, and here it is for anyone else who needs it.

function twitify( text )
{
    // replace urls with linked ones
    var t2 = text.replace(/(http|https)(:\/\/)([^ ]+)/ig, '<a href="$1$2$3">$1$2$3</a>' );

    // replace @username with clickable twitter link
    t2 = t2.replace(/@([^ ]+)/gi,'<a href="http://twitter.com/$1">@$1</a>');

    // replace hashtags with Twitter searches
    t2 = t2.replace(/#([^ ]+)/gi,'<a href="http://search.twitter.com/search?q=%23$1">#$1</a>');

    return t2;
}

So, feed the tweet into that function, and back out it comes with links, @users and #hastags made clickable.

Did I mention I ♥ regular expressions?

The power of Twitter

16th October 2009 by Jason C. Filed under: Blogging

The other day, the Guardian reported that it had been gagged from reporting on a parliamentary question, to be asked in the commons. It couldn’t say who had issued the “gagging order” (a.k.a. a “super injunction”), why it had been issued, and could only sneak out the fact that it existed because it was about parliamentary business.

This went out on a few high profile blogs, and spread from there. Eventually, after a few hours, the reason for the gag came out – I read about it first on Twitter. It was about a company that had dumped toxic waste off the Ivory Coast. Within hours, the name of the company and their solicitors (Trafigura and Carter-Ruck, respectively) were some of the most used words on Twitter. The culmination of all this attention was that Carter-Ruck withdrew their injunction, because of the huge publicity it was bringing to the story.

And that is the story of how Twitter broke a super-injunction. You can read a more full story here with a very nice visualisation of the story as it developed. Not bad considering most of the stuff on Twitter is about what some stranger’s cat’s had for dinner.