Technology: the only way to be a wizard if Hogwarts won't have you.

Display an RSS headline/link using PHP...anywhere!

Date/Time Permalink: 04/10/06 10:01:43 pm
Category: HOWTOs and Guides

Well, been awhile since we had a "hack of the day". Today, after much moaning and pounding of fists (PHP string-handling functions are obtuse!) I have gotten a simple feed parser up on my home page. Along with the link to the blog proper, it now shows the five most recent posts as linked blog headlines.

"Wow, Penguin Pete! How'd you do that?" Well, I'll show you:


<?php
echo "Latest Posts:<br>";
echo "<blockquote>";
$feed = file('http://example.com/b2evo/rss2.php?blog=1');
$count=0;
global $item_title;
global $item_link;
global $linkfound;
foreach ($feed as $line)
{
    if($linkfound)
    {
      echo "<a href=\"" . $item_link . "\">" . $item_title . "</a>";
      echo "<br>";
    }
    $linkfound=0;
    if (strlen(stristr($feed[$count],"<title>"))
    {
      $item_title=strip_tags($feed[$count]);
    }
    elseif (strlen(stristr($feed[$count],"<link>")))
    {
      $item_link=strip_tags($feed[$count]);
      $linkfound=1;
    }
    $count++;
}
echo "</blockquote>";
 ?>

Replace the "file('http://example.com/b2evo/...." with the path to whatever RSS feed you want to parse. Paste it in any web page from which you can execute PHP, and you're there! Should work the same for any RSS2.0 feed.

This is my humble 'lil hack for the day. In a similar vein is 'whoo's post in the b2evolution forums Which shows how to add a single entry to your signature box in a forum.

Follow me on Twitter for an update every time this blog gets a post.
Stumble it Reddit this share on Facebook

suddenly the moon