Twitter has a widget that you can use to display your latest update in your blog sidebar or elsewhere on your website. But it's...well, not perfect. I found that it was bigger than necessary for the text it contained, and the empty space didn't exactly enhance the look of my blog.

So I wrote my own code to display my Twitter updates. You'll see my two latest updates over in the sidebar. Each begins with a link to the update page (not terribly useful, since you can read the whole update right there anyway, but who knows), followed by the date and time it was posted, and the update text itself.

If there's a link in the text, it is automatically converted into a live link.

Both links open new browser windows.

So, how do I do it? I use CaRP Evolution and this code:

<ul><?php
function LinkMyURL($ii,$fn,$iind,$in,$va,$rv) {
	return preg_replace('#(http://[-\\w./]{1,22})([-\\w./]*)#',
		'<a target="_blank" href="\\1\\2">\\1</a>',
		str_replace('AntoneRoundy: ','',$va['TITLE']));
}

function DoMyConfig() {
	CarpConf('iorder','url,date,mytitle');
	CarpRegisterCallback('','LinkMyURL','handlefield','mytitle');
	CarpConf('cborder','');
	CarpConf('idateformat','j-M@g:ia');
	CarpConf('aidate','</i> - ');
	CarpConf('bi','<li>');
	CarpConf('ai','</li>');
	CarpConf('maxitems',2);
	CarpConf('encodingout','UTF-8');
	CarpConf('biurl','<a target="_blank" href="');
	CarpConf('aiurl','">Go &raquo;</a> ');
}

require_once '/my/path/to/carp/carp.php';
CarpRegisterCallback('','DoMyConfig','cachemiss','');
CarpCacheShow('http://twitter.com/statuses/user_timeline/5934972.rss',
	'blog_twitter');
?></ul>

Here's what all that means (just in case you're interested):

I've created two PHP functions. The first, "LinkMyURL", removes "AntoneRoundy: " from the update text, and uses a little regular expression voodoo to convert URLs to hyperlinks (thus the name "LinkMyURL"). The link text may be truncated to prevent really long URLs from breaking the formatting (just in case Twitter doesn't convert to a tinyurl link sometime -- I don't think they ALWAYS do that).

Reader Comment:
Jeremy Young said:
Thanks Antone, This is heaps better than the Twitter Gadget.
(join the conversation below)

The second, "DoMyConfig", is used to configure CaRP whenever it needs to refresh its cache. I tell CaRP to do that using the call to CarpRegisterCallback where I register it as a callback for the "cachemiss" phase of processing.

The last line, "CarpCacheShow", tells CaRP to download my Twitter status RSS feed (if a fresh copy isn't already in its cache) and display it, storing the formatted output in a file named "blog_twitter".

The contents of the DoMyConfig function are as follows:

The "iorder" setting tells CaRP to display the RSS feed item's link ("url"), date (from pubDate or dc:date), and a custom field I created named "mytitle" (which gets its data from the RSS "title" field -- that's "$va['TITLE']" in LinkMyURL). The call to CarpRegisterCallback on the next line registers my LinkMyURL function as a callback for processing my "mytitle" field.

The empty "cborder" setting tells CaRP not to display any RSS channel data.

The "idateformat" setting says to display the day and month, "@", the hour and minute, and "am" or "pm". "aidate" tells CaRP what HTML code to display after the date (CaRP starts the date with an "<i>" tag by default, do I didn't need to specify that, but I wanted to override the default "</i><br />" setting for what to display after it).

The "bi" and "ai" settings tell CaRP put put each RSS feed item in a "list item" tag (I do that because of how my blog template displays sidebar data).

"maxitems" specifies that two updates should be displayed.

Setting "encodingout" to "UTF-8" ensures that the character encoding matches my blog (otherwise, some characters could get displayed incorrectly).

Finally, the "biurl" and "aiurl" settings tell CaRP what to display before and after the URL. I use these to turn the URL into a hyperlink with "Go »" as the link text.

Twitter is cool, but I like this a lot better than their widget.