如果description
文字不存在,如何跳过RSS Feed中的项?
我尝试使用下面的这一行,但我仍然收到错误,
if($x -> item($i) -> getElementsByTagName('description')) { then proceed }
当我在RSS Feed中的项目看起来像这样时,
<item>
<title>Berlusconi on the brink</title>
<link>http://www.spectator.co.uk/coffeehouse/7375408/berlusconi-on-the-brink.thtml</link>
<guid isPermaLink="false"> http://www.spectator.co.uk/coffeehouse/7375408/berlusconi-on-the-brink.thtml </guid>
<pubDate>Tue, 08 Nov 2011 16:42:03 +0000</pubDate>
</item>
正如您所看到的那样,<description>
没有提供。
以下是我提出的功能......
function feed_reader($url,$limit = 1,$title = null)
{
$xml = ($url); //http://twitter.com/statuses/friends_timeline/321998072.rss
$xmlDoc = new DOMDocument();
$xmlDoc -> load($xml);
# Get and output "<item>" elements.
$x = $xmlDoc -> getElementsByTagName('item');
# Count the total feed with xpath.
$xpath = new DOMXPath($xmlDoc);
$total_feed = $xpath->evaluate('count(//item)');
# Set feed limit.
$limit_feed = $limit;
# Check if the total feed is less than the limit then use the total feed as the limit.
if($limit_feed >= $total_feed) $limit_feed = $total_feed;
# Set the variable.
$output = null;
for ($i=0; $i<$limit_feed; $i++)
{
# Make sure that the description node exist then process.
if($x -> item($i) -> getElementsByTagName('description'))
{
$item_title = $x -> item($i) -> getElementsByTagName('title') -> item(0) -> childNodes -> item(0) -> nodeValue;
$item_link = $x -> item($i) -> getElementsByTagName('link') -> item(0) -> childNodes -> item(0) -> nodeValue;
$item_date = $x -> item($i) -> getElementsByTagName('pubDate') -> item(0) -> childNodes -> item(0) -> nodeValue;
$item_description = $x -> item($i) -> getElementsByTagName('description') -> item(0) -> childNodes -> item(0) -> nodeValue;
# NOTE: use this code for the server runs PHP5.3
# DateTime::add — Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
$date = new DateTime($item_date);
# change the date format into Y-m-d H:i:s
$item_date = $date -> format('j F Y');
# count time ago from the published date
$time_ago = time_ago($date -> format('Y-m-d H:i:s'),'d M Y \a\t H:i');
if($title) $output .= '<li><p>'.preg_replace('/^('.$title.':)/', ' ',limit_length(strip_tags($item_description), 200)).'<br/>'.$time_ago.'</p></li>';
else $output .= '<li><p>'.limit_length(strip_tags($item_description), 200).'<br/>'.$time_ago.'</p></li>';
}
}
return $output;
}
答案 0 :(得分:0)
getElementsByTagName()
即使为空,也会返回DOMNodeList
。你需要检查它是否为空。或者,您可以使用xpath,如
$xpath = new DOMXPath($x->ownerDocument);
if ($xpath->evaluate('count("description")', $x)) { ...
答案 1 :(得分:0)
只需使用单个xpath查询即可返回所有具有描述的项目(因为您只查看这些项目):
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
$xpath = new DOMXPath($xmlDoc);
$items = $xpath->query('/rss/item/description/..');
# Set the variable.
$output = '';
foreach($items as $number => $item)
{
if ($number+1 >= $limit_feed) break; # enough items, limit reached
# process each item as you see fit:
echo $xmlDoc->saveXML($item), "\n"; # debug only for demo
$item_title = $item->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
# and so on ....
if ($title)
$output .= '<li><p>'.preg_replace('/^('.$title.':)/', ' ',limit_length(strip_tags($item_description), 200)).'<br/>'.$time_ago.'</p></li>';
else
$output .= '<li><p>'.limit_length(strip_tags($item_description), 200).'<br/>'.$time_ago.'</p></li>';
}
return $output;