2011-11-26 85 views
1

解析元数据我想extact的IMG SRC值了以下RSS源(仅低于偏馈)的。在RSS提要PHP

我目前使用的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> 

回答

0

这是XML CDATA元素中的HTML(XML)。 XML解析器不解析CDATA (character data)。您需要按照与其他元素相同的方式提取值。然后,您可以使用正则表达式解析元素值,或者再次使用XML解析器(如果HTML数据是有效的XML)。

+0

谢谢。这将工作$ desc_imgsrc = $ item ['METADATA:DESCRIPTION']; – ChicagoDude

0
$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'); 
}