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. )

0 thoughts on “Fun with PHP and WordPress”

  1. thanks for explaining that so well.
    I’ve just been playing with parsing files so I can put the latest results on my blog. I did it without going into the wordpress code other than using an include().

    can you explain the require_once(ABSPATH . WPINC . ‘
    parts? abspath is obvious, I’me sure i’ve seen that in the WP code, what do the dots and wpinc mean?

  2. require_once = A require is like an include, but it stops the script if it can’t find the file. The “once” part means that if it’s already included the file, it won’t try to include it again.

    ABSPATH = WordPress’s variable to hold the location of the blog directory.

    WPINC = WordPress’s variable to hold the location of the wp-include directory, relative to the blog directory.

    /rss-functions.php = The name of the file we want.

    The dots are string concatenation operations. So we’re building the location of the file and then including it, basically. Pretty straightforward, really.

  3. Thanks for this!

    I’m trying to put it into my site… using WordPress and a K2 mod… trying to put it into my sidebar PHP module, but I’m not having any luck.

    I’m thinking the paths aren’t correct for my site? I tried messing around with them, but no luck. That rss-functions.php files is in wordpress/wp-includes ,but I’m not sure if it is being referenced properly.

    any help would be appreciated! I’m (obviously) a PHP beginner.

    thanks!!

    -Matt

  4. Actually, at the time I wrote this, doing it this way made sense. Nowadays, what with Yahoo’s new Pipes service, it doesn’t make as much sense. I created pipes to eliminate those ads from those feeds in about 15 seconds each. Now those feeds are available through Yahoo Pipes quite easily, to everybody.

    Go over there and search for “scores” and you’ll find them quite easily:
    http://pipes.yahoo.com/search?q=scores&x=0&y=0

Leave a Reply to Otto Cancel reply

Your email address will not be published. Required fields are marked *

css.php