2010-02-28 93 views
2

我試圖從HTML字符串中提取所有img標籤。看到代碼PHP preg_match_all什麼也沒有返回

$d1  = file_get_contents("http://itcapsule.blogspot.com/feeds/posts/default?alt=rss"); 
preg_match_all('/<img[^>]+>/i',$d1,$result); 
print_r($result); 

,其結果是

Array ([0] => Array ()) 

但同樣的正則表達式給出正確的結果在網上正則表達式測試工具http://regex.larsolavtorvik.com/

可能是什麼問題?

回答

1

你正在分析的內容進行編碼的HTML實體 - 基本上<被替換爲&lt;。首先使用html_entity_decode將數據轉換爲正常的html。 PS:使用HTML解析器而不是正則表達式。

+0

謝謝!會嘗試 – Orion 2010-02-28 20:14:27

0

使用了SimplePie XML解析器

include_once 'simplepie.inc'; 

$feed = "feedurl"; 

$data  = new SimplePie($feed); 
$data->init(); 
$data->handle_content_type(); 

foreach ($data->get_items() as $item) 
{ 
    $desc=$item->get_description(); 
    preg_match_all('/<img[^>]+>/i',$desc,$result); 
    print_r($result); 
} 

這正是我一直在尋找:)謝謝你們解決這個問題!