Displaying Twitter Updates on a Web Page

I recently began using Twitter, a microblogging service for posting short, chat-like blog entries and reading what other users of the service are doing. The site has severe reliability problems, but it's still an entertaining way to get real-time updates from bloggers I read along with others I know who've been sucked into Twitter's maw.

I wrote some code to display my most recent Twitter update on my weblog, Workbench, in a sidebar at upper right. This afternoon, I've released the Twitter-RSS-to-HTML PHP script under an open source license. The script requires MagpieRSS for PHP, an open source PHP library that can parse RSS and Atom feeds.

MagpieRSS caches feed data, so at times when Twitter is glacially slow or can't be accessed, this script won't hurt the performance of your server.

The first release of the script only works with a Twitter user's RSS feed, which can be found in the "RSS" link at the bottom of a user's Twitter page. The only tough part about writing the script was creating regular expressions to turn URLs into hyperlinks and "@" references into links to Twitter user pages:

// turn URLs into hyperlinks
$tweet = preg_replace("/(http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*)/", "<a href=\"\\0\">Link</a>", $tweet);
// link to users in replies
$tweet = preg_replace("(@([a-zA-Z0-9]+))", "<a href=\"http://www.twitter.com/\\1\">\\0</a>", $tweet);

If you're reading this and wondering why anyone should bother with Twitter, I recommend reading the updates by Jay Rosen, a former university journalism chair who uses the service to share a running dialogue on the media. He punches above his weight in this 140-character-or-less medium.


Thanks, Rogers! I appreciate the support, and the recommendation. By the way, I am not chair of the Journalism program any longer. I was from 1999-2004.


Thanks for the correction. Fixed.

Ah, but the question is not "anyone", but more at "anyone who is not a heavily-connected quasi-marketer". What Jay Rosen has specialized in at times, is crafting sales-pitches for the "Web 2.0" crowd, with regard to stroking them about media issues. Sure, those people - the Usual Suspects - will love Twitter. But why should anyone ELSE bother? Especially those not in the business I call serving the A-list.

I like Rosen because of what he says about journalism, not stuff inside the Silicon Valley bubble. A lot of the news on TechCrunch/Techmeme/ReadWriteWeb bores the hell out of me -- just a bunch of venture capitalists and programmers chasing money.

But a large part (sigh, not all) of what he says about journalism is basically that the TC/TM/RWW crowd is the future, and journalist should look to them for the model to follow. So of course they (the TC/TM/RWW crowd) absolutely love him for it. Which is why he has such Twitter popularity. My point is that anyone who was not an A-list favorite would not have such weight, and that's almost everyone.

That is, he's something of a ringer in your example. He's not just "a former university journalism chair", but a full club member of Kool-Aid Inc.

[Tedious: Jay, I know you've written a bazillion words, this is a brief comment, it necessarily must gloss over your oeuvre.]

I just hope Twitter bulks up its service soon because I have been having problems with it being down a lot over the past couple of months. not sure what the problem is other than maybe they can not keep up with the amount of data being transfered over there network.

i am the loyal fans of http://www.tvokay.com
just go to http://www.tvokay.com watch full movies and TV shows...
the best movies and tv-shows at www.tvokay.com <>

I know I'm joining this thread pretty late, but it seems that my search on google pointed me here first.

I'd like to point out the underscore '_' character that's being used on twitter.

You just have to modify the preg_match to: (@([a-zA-Z0-9_]+))

All I did was add the _ at the end of the 9. Seems to work fine for me. Thanks for posting this up!

Add a Comment

All comments are moderated before publication. These HTML tags are permitted: <p>, <b>, <i>, <a>, and <blockquote>. This site is protected by reCAPTCHA (for which the Google Privacy Policy and Terms of Service apply).