解析RSS提要中的元数据


Parsing meta data in RSS feed PHP

我正试图从下面的RSS提要(下面只有部分提要)中提取IMG SRC值。

我目前使用XML解析器来获取其余的项目-它工作得很好(例如):

foreach($xml['RSS']['CHANNEL']['ITEM'] as $item) 
{
...
            $title = $item['TITLE'];
            $description = $item['DESCRIPTION'];
            $link = $item['LINK'];
        $desc_imgsrc = <how do i get this for below RSS feed??>;
...
}

然而-我如何得到IMG SRC值从下面的RSS提要到一个PHP变量?具体来说,我试图提取"http://thumbnails.---.com/VCPS/sm.jpg"字符串到上面的$desc_imgsrc变量?我怎么能适应上面的代码做到这一点?谢谢。

<item>
<title>Electric Cars - all about them</title>
<metadata:title xmlns:metadata="http://search.--.com/rss/2.0/Metadata">This is the title metadata</metadata:title>
<description>This is the description</description>
<metadata:description xmlns:metadata="http://search.---.com/rss/2.0/>
<![CDATA[<div class="rss_image" style="float:left;padding-right:10px;"><img border="0" vspace="0" hspace="0" width="10" src="http://thumbnails.---.com/VCPS/sm.jpg"></div><div class="rss_abstract" style="font:Arial 12px;width:100%;float:left;clear:both">This is the description</div>]]></metadata:description>
<pubDate>Fri, 25 Nov 2011 07:00 GMT</pubDate>

这是XML CDATA元素中的HTML (XML)。XML解析器不解析CDATA(字符数据)。您需要以与处理其他元素相同的方式提取值。然后,您可以通过使用正则表达式或更好地再次使用XML解析器(如果HTML数据是有效的XML)来解析元素值。

$doc = new DomDocument;
@$doc->loadHTML(...); // html string
// use @ to supress the warning due to mixture of xml and html
$items = $doc->getElementsByTagName('img');
foreach ($items as $item)
{
  $src = $item->getAttribute('src');
}