这是我的代码:
<?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();
?>
运行此脚本时,第一项显示第二项 pubDate 。第二项显示第三项的 pubDate ,依此类推。因此,显示的日期不是您在原始XML文件中看到的日期。我该如何解决这个问题?
额外问题:如何从 pubDate 标记的开头和结尾删除字符,以便我最终得到“2010年5月15日”而不是“周六,2010年5月15日03:28 :00-0700“?
答案 0 :(得分:1)
我已经说过before,所以我再说一遍:使用Magpie RSS来解析您的RSS Feed。它会为您处理所有这些事情,并且会更加可靠。
答案 1 :(得分:0)
Magpie RSS很棒。这是我用来替换原始问题的代码:
<?php
define('MAGPIE_INPUT_ENCODING', 'UTF-8');
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
//Tell it to use the fetch script to grab the RSS feed
require_once('magpie/rss_fetch.inc');
//Now it knows how to fetch RSS, tell it which one to fetch
$rss = fetch_rss('http://samnabi.posterous.com/rss.xml');
//In this case, we only want to display the first 3 items
$items = array_slice($rss->items,0,3);
//Now we tell Magpie how to format our output
foreach ($items as $item) {
$title = $item['title'];
$date = date('d M Y', strtotime($item['pubdate']));
$link = $item['link'];
$description = $item['description'];
//And now we want to put it all together.
echo "<p>$date</p><h2>$title</h2><p>$description</p>";
}
?>