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;

Tagged with:
 

9 Responses to Random RSS Item with PHP and ICONV for formatting.

  1. admin says:

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

  2. William says:

    Hey Thanks for the script and the information very useful.
    I hope you have a very safe, and Wonderful New Year.

  3. Mike says:

    Nice script. But i dont think you can use multiple return commands inside a single function.

  4. Scott says:

    Hi Ed

    I was playing around with your little script and get the following error:

    Parse error: syntax error, unexpected T_STRING in /home/scottpar/public_html/spgraphic/jackie1/rss.php on line 21

    This is line 21:

    $title = iconv(“UTF-8?, “ISO-8859-1?, $title);

    Wondered if you had any ideas?

    Thanks

    Scott

  5. admin says:

    Yep copy and pasting strikes – you have ? instead of ” right after the iso type.

  6. admin says:

    For all of you driving me nuts about this snippet please note that for some strange reason WP is converting quotes into strange characters. Just replace them and it works fine.

  7. Steve says:

    do you have an example page of where you have this code installed? I can’t seem to figure out how to properly place it on a php page and display any results. Thanks

  8. Steve says:

    never mind… I read through and looked at the code and noticed it copy and pasted incorrect characters for the “, the ‘, and the ?. I got it working just fine now. Sweet Code, thanks!

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>