2013-06-05 26 views
0

我有一個RSS提要,我試圖通過SimplePie提取數據(在WordPress中)。從SimplePie中提取特定數據get_content對象

我必須提取內容標籤。它適用於<?php echo $item->get_content(); ?>。它拋出這一切的東西(當然這只是一個入口,其他具有相同的結構):

<table><tr valign="top"> 
<td width="67"> 
<a href="http://www.anobii.com/books/Lapproccio_sistemico_al_governo_dellimpresa/9788813230944/014c5c45a7ddaab1ec/" style="border: 1px solid #333333"> 
<img src="http://image.anobii.com/anobi/image_book.php?type=3&amp;item_id=014c5c45a7ddaab1ec&amp;time=0"> 
</a> 
</td><td style="margin-left: 10px;padding-left: 10px">[person name] put "[title]" onto shelf<br/></td></tr></table> 

雖然我需要的只是裏面的src =「」標籤(圖像URL)的內容。我怎樣才能提取只?

回答

2

則可以使用DOMDocument(最好的方式)做:

$doc = new DOMDocument(); 
@$doc->loadHTML($html); 
$imgs = $doc->getElementsbyTagName('img'); 
$res = $imgs->item(0)->getAttribute('src'); 

print_r($res); 

隨着一個正則表達式(糟糕的方法):

if (preg_match('~\bsrc\s*=\s*["\']\K[^"\']*+~i', $html, $match)) 
    print_r($match); 
+0

所以我應該打印$用品 - > get_content();首先在一些文件上? – MultiformeIngegno

+0

@MultiformeIngegno:不,只需用'$ item-> get_content()替換'$ html'' –

+0

就像一個魅力! :D – MultiformeIngegno