当使用rss feed.I时,如何在simplepie中只显示图像?我正在使用simplepie,但允许从rss.How中仅提取图像来执行此操作?
发布于 2012-06-07 03:57:32
考虑到某些提要结构是不同的,据我所知,还没有明确的方法来做到这一点。这就是我是如何做到这一点的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; ?>这可能需要一些but,但是一旦你知道你的img标签在提要中的位置,你就可以让simplepie解析提要,函数会找到标签和格式,所以它是html格式的。
如果只想拉出一个帖子,你可以编辑你的循环:
foreach ($feed->get_items(0 , 1) as $item): 第一个数字(0)是起点,0是最新的帖子。第二个数字(1)是拉取多少帖子。因为您只想要一个图像,所以您希望该数字为1。
发布于 2012-06-20 23:48:01
也许你想在客户端做这件事。然后,使用jQuery就很容易了。假设javascript中的item-content如下所示
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上的文档。
通过这种方式,我删除了一些元素,在内容中添加了一些或添加了一些类,以便以我的方式显示帖子。
https://stackoverflow.com/questions/10577746
复制相似问题