Here is my code:
<?php
$RSSFEEDS = array(
0 => "http://samnabi.posterous.com/rss.xml",
);
function FormatRow($date, $title, $link, $description) {
return <<<HTML
<p class="blogdate">$date</p><h2 class="blogtitle">$title</h2>
<div class="clearer"> </div>
$description
HTML;
}
ob_start();
if (!isset($feedid)) $feedid = 0;
$rss_url = $RSSFEEDS[$feedid];
$rss_feed = file_get_contents($rss_url);
$rss_feed = str_replace("<![CDATA[", "", $rss_feed);
$rss_feed = str_replace("]]>", "", $rss_feed);
$rss_feed = str_replace("\n", "", $rss_feed);
$rss_feed = preg_replace('#<image>(.*?)</image>#', '', $rss_feed, 1 );
preg_match_all('#<pubDate>(.*?)</pubDate>#', $rss_feed, $date, PREG_SET_ORDER);
preg_match_all('#<title>(.*?)</title>#', $rss_feed, $title, PREG_SET_ORDER);
preg_match_all('#<link>(.*?)</link>#', $rss_feed, $link, PREG_SET_ORDER);
preg_match_all('#<description>(.*?)</description>#', $rss_feed, $description, PREG_SET_ORDER);
if(count($title) <= 1) {
echo "No new blog posts. Check back soon!";
}
else {
for ($counter = 1; $counter <= 3; $counter++ ) {
if(!empty($title[$counter][1])) {
$title[$counter][1] = str_replace("&", "&", $title[$counter][1]);
$title[$counter][1] = str_replace("'", "'", $title[$counter][1]);
$row = FormatRow($date[$counter][1],$title[$counter][1],$link[$counter][1],$description[$counter][1]);
echo $row;
}
}
}
ob_end_flush();
?>
When this script is run, the first item displays the second item's pubDate. The second item displays the third item's pubDate, and so on. So the dates that are shown are not the dates that you see in the original XML file. How do I fix this?
Bonus question: how do I strip characters off the beginning and end of the pubDate tag, so that I end up with "15 May 2010" instead of "Sat, 15 May 2010 03:28:00 -0700" ?