【发布时间】:2016-12-08 08:57:26
【问题描述】:
我在我的网站上测试的 RSS 阅读器的代码方面需要帮助,脚本工作正常,但它显示 20 个提要,我想将其限制为我设置的数字(例如 3 或 6)。
代码是这样的:
<?php
//Feed URLs
$feeds = array(
"https://robertsspaceindustries.com/comm-link/rss",
);
//Read each feed's items
$entries = array();
foreach($feeds as $feed) {
$xml = simplexml_load_file($feed);
$entries = array_merge($entries, $xml->xpath("//item"));
}
//Sort feed entries by pubDate
usort($entries, function ($feed1, $feed2) {
return strtotime($feed2->pubDate) - strtotime($feed1->pubDate);
});
?>
<ul><?php
//Print all the entries
foreach($entries as $entry){
?>
<li><a href="<?= $entry->link ?>"><?= $entry->title ?></a> (<?= parse_url($entry->link)['host'] ?>)
<p><?= strftime('%m/%d/%Y %I:%M %p', strtotime($entry->pubDate)) ?></p>
<p><?= $entry->description ?></p>
<img src="<?= $entry->children('media', true)->content->attributes()->url ?>" alt="" />
</li>
<?php
}
?>
</ul>
我尝试使用变量搜索解决方案,但我失败了... 谢谢您的帮助! :)
【问题讨论】:
-
如果需要,只需在
foreach上添加break,$i = 0; foreach(){$i++; if i = 3 break}或剪切阵列、切片或拼接
标签: php html xml rss simplexml