0

我想在标题开始之前删除所有图像标签,但它们的嵌套方式不同。然后删除空标签。

<div class="c2">
  <img src="image/file" width="480" height="360" alt="Image" />
</div>
<div class="c2">
  <div class="headline">
    headline
  </div>
  <div class="headline">
    headline2
  </div>
</div>

和不同的嵌套标签,如

<div class="c2">
  <p>
    <img src="image/A.JPG" width="480" height="319" alt="Image" />
  </p>
  <div class="headline">
    A headline
  </div>
</div>

我认为这可以递归解决,但我不知道如何。

谢谢你的帮助!

4

1 回答 1

0

编辑:如果您只想删除<img>后跟<div><div class="headline>"or <div class="headline">,请使用此 xpath:

$imgs = $xpath->query("//img[../following-sibling::div[1]/div/@class='headline' or ../following-sibling::div[1]/@class='headline']");

看到它工作:http ://codepad.viper-7.com/QhprLP

像这样做:

$doc = new DOMDocument();
$doc->loadHTML($x); // assuming HTML in $x
$xpath = new DOMXpath($doc);
$imgs = $xpath->query("//img"); // select all <img> nodes

foreach ($imgs as $img) { // loop through list of all <img> nodes
$parent = $img->parentNode; 
$parent->removeChild($img); // delete <img> node
if ($parent->childNodes->length >= 1) // if parent node of <img> is empty delete it
        $parent->parentNode->removeChild($parent);
}

echo htmlentities($doc->saveHTML()); // display the new HTML

看到它工作:http ://codepad.viper-7.com/350Hw6

于 2013-05-09T20:42:02.380 回答