【问题标题】:Grab only the latest 3 posts from RSS feed [duplicate]仅从 RSS 提要中获取最新的 3 篇文章 [重复]
【发布时间】:2013-11-01 19:07:00
【问题描述】:

我下面的代码正在获取整个 RSS 提要,我怎样才能将其限制为仅检索最新的 3 个帖子?要么只显示最新的 3 个帖子,而不是所有帖子。

<?php
$xml=simplexml_load_file("http://tutorial.world.edu/feed/");
foreach ($xml->channel->item as $item) {
    $title = (string) $item->title; // Title Post
    $link   = (string) $item->link; // Url Link
    $pubDate   = (string) $item->pubDate; // date
    $description = (string) $item->description; //Description Post
echo '<div class="display-rss-feed"><a href="'.$link.'" target="_blank" title="" >'.$title.' </a><br/><br/>';
echo $description.'<hr><p style="background-color:#e4f;">'.$pubDate.'</p></div>';
 }
?>

【问题讨论】:

    标签: php html xml rss


    【解决方案1】:

    一个简单的方法是计数。从零开始,每次迭代计数,如果计数器达到四,您只需退出循环。

    另一种可能性是在 IteratorIterator 上使用 LimitIterator Traversable 您有问题 ($xml-&gt;channel-&gt;item)。

    此处概述:

    与您的问题进一步相关的是:

    【讨论】:

      【解决方案2】:

      您可以使用 array_slice($xml->channel->item, 0, 3) 来获取最多 3 个第一个帖子

      【讨论】:

      • 不起作用不是数组。
      猜你喜欢
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多