Fun with PHP and WordPress

Guy over here asked how to display hockey scores on his blog. Here?s my solution?

Edit: Fixed a minor problem with the regular expression. Some of the soccer team names caused problems on that feed.

A quick googling tells me that exactscores.com seems to have feeds for scores for lots of stuff, however they also put horrible amounts of advertising into the RSS feed. Nevertheless, that can be worked around, if you want. )

The actual score information in the feeds comes in the titles, and it looks like this:

(No)001/1 (BST+2h,Start at)03:00 (Home Team)EDM OILERS (Final)3-6 (Away Team)ANA MIGHTY DUCKS (Status)Finished (First Period)0-3 (Second Period)3-2 (Third Period)0-1 (Full Time)3-6 (Over Time)- (Penalties)- (Scorers)LUPUL(19)

Now, that is pretty ugly. But, at least it?s in a semi normalized format: (Key)Value? The Scorers at the end isn?t pretty, but we can deal with that later. Also, thankfully, the actual scores in all the ones I checked always start with (No), so we can use that to determine which bits of the feed are actual scores and which are not.

So here?s a simple script to retrieve a feed and then display only those bits that start with (No):

< ?php
require_once(ABSPATH . WPINC . '/rss-functions.php');
$rss = fetch_rss('http://www.exactscores.com/HockeyLivescoreRss.xml');
echo '
    '; foreach ($rss->items as $item ) { if (preg_match('/^(No).*/i',$item['title'])) echo '
  • '.$item['title'].'
  • '; } echo ''; ?>

Obviously, the output of this is ugly, but it proves that we?re only getting scores out of the feed.

So, now we need to parse that string and get more useful data out of it. Here?s where preg_split and some rather disturbing regular expression syntax is your friend?

$uglyscores = preg_split('/^(([^)]*))[^s]|s(([^)]*))[^s]/',
	$item['title'], -1, PREG_SPLIT_DELIM_CAPTURE);

I called it $uglyscores for a reason. This returns an array with the bits of the scores nicely parsed out, but because of the regular expression I used, there?s a blank string between each pair of data, so let?s reformat this mess into something nicer:

$i=1; // skip the first element
$scores = array();
while ($i<count ) {
	$scores[$uglyscores[$i]]=$uglyscores[$i+1];
	$i+=3; // skip the blank separator
}

This gives us a nice array of elements that we can then reference by the key names. So we can use stuff like $scores[?Final?] and such.

So now that we don?t have to display everything, let?s put it all together. Here?s a combination of all of the above that outputs the two teams and the final score only:

< ?php
require_once(ABSPATH . WPINC . '/rss-functions.php');
$rss = fetch_rss('http://www.exactscores.com/HockeyLivescoreRss.xml');
echo '
    '; foreach ($rss->items as $item ) { if (preg_match('/^(No).*/i',$item['title'])) { echo '
  • '; $uglyscores = preg_split('/^(([^)]*))[^s]|s(([^)]*))[^s]/', $item['title'],-1,PREG_SPLIT_DELIM_CAPTURE); $i=1; // skip the first element $scores = array(); while ($i<count ) { $scores[$uglyscores[$i]]=$uglyscores[$i+1]; $i+=3; // skip the blank separator } echo "Home Team: ".$scores["Home Team"].""; echo "Away Team: ".$scores["Away Team"].""; echo "Final Score: ".$scores["Final"]; echo '
  • '; } } echo ''; ?>

And there you have it. If you want to display different information, just modify the stuff in those echo lines towards the end.

The key is finding the RSS feed with the data you?re interested in. Once you have that, it?s usually fairly straightforward to parse it and redisplay it nicely. )

Happy Towel Day!

A towel is about the most massively useful thing an interstellar hitch hiker can have. Partly it has great practical value – you can wrap it around you for warmth as you bound across the cold moons of Jaglan Beta; you can lie on it on the brilliant marble-sanded beaches of Santraginus V, inhaling the heady sea vapours; you can sleep under it beneath the stars which shine so redly on the desert world of Kakrafoon; use it to sail a mini raft down the slow heavy river Moth; wet it for use in hand-to-hand-combat; wrap it round your head to ward off noxious fumes or to avoid the gaze of the Ravenous Bugblatter Beast of Traal (a mindboggingly stupid animal, it assumes that if you can’t see it, it can’t see you – daft as a bush, but very ravenous); you can wave your towel in emergencies as a distress signal, and of course dry yourself off with it if it still seems to be clean enough.

More importantly, a towel has immense psychological value. For some reason, if a strag (strag: non-hitch hiker) discovers that a hitch hiker has his towel with him, he will automatically assume that he is also in possession of a toothbrush, face flannel, soap, tin of biscuits, flask, compass, map, ball of string, gnat spray, wet weather gear, space suit etc., etc. Furthermore, the strag will then happily lend the hitch hiker any of these or a dozen other items that the hitch hiker might accidentally have “lost”. What the strag will think is that any man who can hitch the length and breadth of the galaxy, rough it, slum it, struggle against terrible odds, win through, and still knows where his towel is is clearly a man to be reckoned with.

A tribute to Douglas Adams (1952-2001)

40 years of X-Men

x-menCheck this out.

Yeah, thatÂ’s all forty years of X-Men, on DVD, in a printable format, for only $40. (Update: $33 now!)
485 comics. 17,500 pages.

How freakinÂ’ awesome is that?

There’s also a version that comes with the Ultimate X-Men on DVD as well. It’s $50 for the bundle of both of them. Here’s a link to the bundle.

Seems like a hell of a good deal, for an X-Men fan (like everybody should be!).

Whole body pain

Is this some sort of consequence of being 30 again? This post-drinking weekend whole body pain thing? Because if it is, then I’m going to have to start researching high quality medications again. My ankle is hurt, the muscles in my back are strained, I can’t turn my neck… all because of the music festival.

To make it even more fun, my A/C and icemaker both chose this weekend to stop working for no appearant reason. So it was like 85 in my apartment all weekend. That tended to drive us outside and drink, though, so in that respect it wasn’t so bad. I’ve fixed the icemaker now, they’re fixing the A/C today.

Anyway, it was a good show. I enjoyed Cake and Blues Traveller a lot. Yellowcard was surprisingly good when they did a Weezer cover. I sat around the Blues Tent for a few hours on Sunday and enjoyed some beer and blues from several artists as well. Also ate a lot of BBQ. So it was good weekend in that respect. Beale Street was fun and had a lot of interesting characters. One dude we saw walking around dressed up as Gene Simmons ended up on the front page of the Sunday paper. )

So it was fun. But I still need to find drugs.

Wolfgang Puck is pissed!

Heh. I blogged about those Wolfgang Puck self-heating coffee things about a year ago. Well, now it seems that they’re being recalled due to reports of some of them exploding and such. I’ve gotten them a few times since then, and never had any problems with them, but still, I can see how one in a thousand might have an issue. The things get very, very hot, and if you don’t open the can when it does get hot enough, it’s possible that it could overheat. Or possibly poor manufacturing and quality control is the cause. Whatever, the point is that they’re appearantly dangerous. )

css.php