0
我正在建立一個網站來學習PHP,並且正在製作一個可以在我的網站上展示的feed-rss-feeds。如何限制飼料?
下面的代碼:
<?php
class Feed_Amalgamator
{
public $urls = array();
public $data = array();
public function addFeeds(array $feeds)
{
$this->urls = array_merge($this->urls, array_values($feeds));
}
public function grabRss()
{
foreach ($this->urls as $feed)
{
$data = @new SimpleXMLElement($feed, 0, true);
if (!$data)
throw new Exception('Could not load: ' . $feed);
foreach ($data->channel->item as $item)
{
$this->data[] = $item;
}
}
}
public function amalgamate()
{
shuffle($this->data);
$temp = array();
foreach ($this->data as $item)
{
if (!in_array($item->link, $this->links($temp)))
{
$temp[] = $item;
}
}
$this->data = $temp;
shuffle($this->data);
}
private function links(array $items)
{
$links = array();
foreach ($items as $item)
{
$links[] = $item->link;
}
return $links;
}
}
/********* Example *********/
$urls = array('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/teams/m/man_city/rss.xml', 'http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/teams/l/liverpool/rss.xml');
try
{
$feeds = new Feed_Amalgamator;
$feeds->addFeeds($urls);
$feeds->grabRss();
$feeds->amalgamate();
}
catch (exception $e)
{
die($e->getMessage());
}
foreach ($feeds->data as $item) :
extract((array) $item);
?>
<a href="<?php echo $link; ?>"><?php echo $title; ?></a>
<p><?php echo $description; ?></p>
<p><em><?php echo $pubDate; ?></em></p>
<?php endforeach; ?>
這是一個偉大的劇本,完美的作品,但它佔用了相當多的空間在我的網站。我怎麼能限制它只顯示5個結果,有點像MySQL限制?
您想要總共五個結果,或者每個URL有五個結果? – j08691 2012-08-14 16:52:19
@ j08691,5個總計結果 – Muhambi 2012-08-14 16:52:45
或者每個網址的結果比2更容易。這可能嗎? – Muhambi 2012-08-14 16:58:32