Random RSS Item with PHP and ICONV for formatting.
So I had to hack together a little snippet today that would return just one single item from a rss feed and have it be random from the available items.
(For example this snippet of code will return one random rss item from blackhatdigest – skipping the most recent entry)
function load_xml_feed($location)
{
global $value1;
$feedXml = simplexml_load_file($location);
$i= ‘1′;
foreach ($feedXml->channel->item as $article)
{
$title[$i] = (string)$article->title;
$link[$i] = (string)$article->link;
$description[$i] = (string)$article->description;
$i++;
}
$randnumber = rand(2, $i);
$link = trim($link[$randnumber]);
$title = trim($title[$randnumber]);
$description = trim($description[$randnumber]);
$title = iconv(“UTF-8″, “ISO-8859-1″, $title);
$description = iconv(“UTF-8″, “ISO-8859-1″, $description);
$value1 = array($title,$link,$description);
return $value1;
}
$rss = ‘http://feeds.feedburner.com/BlackHatSeoDigest’;
load_xml_feed($rss);
$link = $value1[1];
$title = $value1[0];
$description = $value1[2];
echo $link;
echo $title;
echo $description;
Cash in on your Competitors’ Work – SpyFu Ad History
Popularity: 2% [?]






December 30th, 2009 at 5:15 am
If you want to include the most recent item from the feed as well you just need to change this line:
$randnumber = rand(2, $i);
Make it:
$randnumber = rand(1, $i);
December 31st, 2009 at 4:57 am
Hey Thanks for the script and the information very useful.
I hope you have a very safe, and Wonderful New Year.
December 31st, 2009 at 12:41 pm
Nice script. But i dont think you can use multiple return commands inside a single function.
December 31st, 2009 at 7:27 pm
Mike,
Bad copy and paste — but oddly enough that code worked. It shouldn’t have, but it did.