如何使用PHP从HTML代码块中删除<img/>标记?

时间:2012-11-25 02:42:46

标签: php regex

我想仅从html中删除特定网址中的图片

例如: 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>

我想从another-domain.tld中删除图片 并保留另一张图片。

由于

1 个答案:

答案 0 :(得分: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