2012-11-25 34 views
1

我只想從HTML如何使用PHP從HTML代碼塊中刪除<img>標記?

刪除特定的URL形象的比方: http://pastebin.com/Qaw4dRbT

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.<img src="http://www.another-domain.tld/r/151230695794/32310/s/25e829c1/removeit.img" alt="" width="1" height="1" border="0" /></p> 

我想從另一個-使用domain.tld 刪除圖像,並保持其他圖像。

感謝

+1

使用http://www.php.net/manual/en/ book.simplexml.php和各種。 – soulseekah

+0

請不要使用正則表達式來解析/處理HTML,而是使用[HTML解析器](http://php.net/manual/en/class.domdocument.php)。 – PeeHaa

回答

4

找到它使用XPath和從其父將其刪除:

// Build a new DOMDocument, load it up with your HTML 
$doc = new DOMDocument(); 
$doc->loadHTML($html); 

// Reference to our DIV container 
$container = $doc->getElementsByTagName("div")->item(0); 

// New instance of XPath class based on $doc 
$xpath = new DOMXPath($doc); 

// Get images that contain 'specific-domain.tld' in their src attribute 
$images = $xpath->query("//img[contains(@src,'specific-domain.tld')]"); 

// For every image found 
foreach ($images as $image) { 
    // Remove that image from its parent 
    $image->parentNode->removeChild($image); 
} 

// Output the resulting HTML of our container 
echo $doc->saveHTML($container); 

可執行文件演示:http://sandbox.onlinephpfunctions.com/code...6529d025e135013184e