如何在使用rss提要时在simplepie中显示唯一图像。我正在使用simplepie,但只能从rs提取图像。如何执行此操作?使用rss提要在simplepie中仅显示图像
1
A
回答
2
考虑到某些Feed结构不同,我没有明确的方式来做到这一点,我知道。这是我能够做到it.First看看这篇文章:
How to Display Thumbnail from WordPress RSS feed using SimplePie?
我能够复制并粘贴代码,并
我没有使用Wordpress做一些细微的变化,但它帮助我了解需要做些什么。
在feed-> init()后插入此代码。
//This function will get an image from the feed
function returnImage ($text) {
$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
$pattern = "/<img[^>]+\>/i";
preg_match($pattern, $text, $matches);
$text = $matches[0];
return $text;
}
//This function will filter out image url which we got from previous returnImage() function
function scrapeImage($text) {
$pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
preg_match($pattern, $text, $link);
$link = $link[1];
$link = urldecode($link);
return $link;
}
现在你要调用的功能,因此它搜索的说明,或在我的情况的内容,找到img标签,并正确结构输出。这是我的代码看起来像:
foreach ($feed->get_items(0 , 3) as $item):
$feedDescription = $item->get_content();
$image = returnImage($feedDescription);
$image = scrapeImage($image);
$image_url= $item->get_permalink();
$description = $item->get_description();
?>
<div class="item">
<h4><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h4>
<div class="image-box"><?php echo '<a href="' . $image_url . '"><img src="' . $image . '" /></a>'."\n";?></div>
<p><?php echo $description ?></p>
<p><a href="<?php echo $item->get_permalink(); ?>">Continue Reading</a></p>
</div>
<?php endforeach; ?>
这可能需要一些迷恋,但一旦你知道你的img标签的饲料中存在,就可以得到了SimplePie解析饲料和功能会发现标签和格式,所以它是HTML的准备。
如果只拉一个职位,你可以编辑你的循环:
foreach ($feed->get_items(0 , 1) as $item):
第一个数字(0)为出发点,0是最新的帖子。第二个数字(1)是需要拉多少个帖子。由于您只需要一张图片,因此您希望该数字为1.
1
也许您想在客户端执行此操作。然后用jQuery很容易。 比方说,在你的JavaScript您的项目,内容是这样的
var content = '<div><a href="http://kwout.com/cutout/b/es/y2/ukn_rou_sha.jpg"
imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;">
<img border="0" height="239" src="http://kwout.com/cutout/b/es/y2/ukn_rou_sha.jpg"
width="320"></a>Erat austro rerum.</div>';
然后你就可以轻松地识别并与jQuery的选择内容中隔离图像。例如使用
$(content).find('img:first'); //find the FIRST image inside an element or
$(content).find('a:first'); //take the hyperlink AND the image
如果不是里面的内容第一图像或内容比这个例子中(主要是;-)更复杂 - 无后顾之忧 - 然后一些其他选择将这样的伎俩。请参阅jquery.com上的文档以获取更多信息。
通过这种方式,我删除了元素,向内容中的不同元素添加了一些或添加的类,以按照我的方式显示帖子。
相关问题
- 1. 使用SimplePie显示RSS图像
- 2. 使用RSS提要显示图像
- 3. 您是否可以在SimplePie上显示RSS提要?
- 4. SimplePie不显示Google RSS源
- 5. RSS提要显示
- 6. 如何使用Simplepie添加“加载更多”RSS提要功能
- 7. 删除RSS提要图像
- 8. PHP - 特殊字符(欧元符号)显示不正确(RSS提要/ SimplePie)
- 9. c#无法显示RSS提要的图像
- 10. 如何在RSS中显示图像?
- 11. 在PHP中显示XML RSS图像
- 12. rss flux filter simplepie
- 13. RSS提要和图像提取indepth
- 14. 使用simplepie在同一页面的不同列中显示多个提要
- 15. 检索RSS提要并在文本视图中显示
- 16. php rss feed显示图像
- 17. Simplepie RSS - 我只需要在描述中获取图片网址以缩略图形式显示
- 18. RSS提要只显示文本
- 19. RSS提要显示的NaN XML考试
- 20. SimplePie RSS Not getting pubDate
- 21. RSS了SimplePie - get_language在NULL
- 22. 如何在应用中显示RSS提要?
- 23. RSS XML显示iTunes:PHP中的图像url
- 24. 如何删除RSS提要图像
- 25. 从RSS提要拉动图像
- 26. 仅使用javascript显示图像
- 27. 在javascript中使用RSS提要
- 28. 在ASP和C中使用RSS提要
- 29. 使用Simplepie管理RSS缓存fetch_feed()
- 30. 使用PHP来显示RSS提要属性
hmm.nice谢谢。 –