2010-07-04 94 views
0

这里是我的代码:当解析RSS提要,则显示错误的日期

<?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">&nbsp;</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("&apos;", "'", $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”结束“?

回答

1

我说过它before,所以我会再说一遍:使用Magpie RSS解析您的RSS提要。它为你处理所有这些东西,并且会更加可靠。

+0

超级骗子。非常感谢! – 2010-07-04 03:54:47

0

鹊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>"; 
} 

?>