我正试图在我的magento网站中显示RSS提要,但我在显示提要的图像时遇到了困难。通过研究提要,我发现图像位于content:encoded标签中,因此我无法使用$item->image之类的东西直接访问它。这是我当前的代码:
<?php $channel = new Zend_Feed_Rss('newsfeedurl'); ?>
<?php foreach ($channel as $item): ?>
<?php if($i<2) { ?>
<img src="<?php echo $item->image; ?>" title="<?php echo $item->title; ?>" height="63" width="95" />
<?php echo "image:".$item->content; ?>
<?php } else {} ?>
<?php $i++; ?>
<?php endforeach; ?>
$?>
我也尝试过使用$item->content,但这会返回新闻提要的全部内容。所以我的问题是,我如何从内容中访问图像的来源:编码以便在我的提要上显示它?
更新:经过更多的研究,我尝试使用preg_match:preg_match["''']?([^"'''])/I',$item->content,$matches);echo$matches[0];我得到了正确的图像路径,但我把它放在了一个循环中,所以每个图像应该至少有2个图像,但我只得到了1个。为什么会这样?
解决了:我已经设法通过将$matches[0]更改为$matches[1]来解决我的问题。我想我使用0时认为它是数组匹配的索引。
为了从content:encoded标签中获取图像源,我使用了正则表达式(preg_match)。以下是我当前代码的外观:
<?php $channel = new Zend_Feed_Rss('newsfeedurl'); ?>
<?php foreach ($channel as $item): ?>
<?php preg_match('/<*img[^>]*src *= *["'']?([^"'']*)/i', $item->content, $matches); ?>
<?php if($i<2) { ?>
<img src="<?php echo $matches[1]; ?>" title="<?php echo $item->title; ?>" height="63" width="95" /></a></div>
<?php } else {} ?>
<?php $i++; ?>
<?php endforeach; ?>
希望这能帮助其他人。