PHP - 删除包含特定文本的父级

时间:2015-08-27 12:09:19

标签: php xpath extract

HTML:

<div class="vm-customfield-cart">
     <span class="product-field-type-S">Some important text</span><br />
     <span class="product-field-type-S">Lorem ipsum dolor</span><br />
</div>

如何删除包含文本Lorem ipsum的跨度。 Lorem ipsum之后的所有内容都可以变量,这就是我需要按Lorem ipsum搜索jsut的原因。

我的代码如下:

$dom = new DOMDocument;
$dom->loadHTML($html);
$xp = new DOMXPath($dom);

$spanNodeList = $xp->query("//*[contains(., 'Lorem ipsum')]");

foreach ($spanNodeList as $spanNode) {
    //I have no idea how to clean it out
}

最终结果应如下所示:

<div class="vm-customfield-cart">
     <span class="product-field-type-S">Some important text</span><br />
</div>

1 个答案:

答案 0 :(得分:0)

尝试

$spanNode->parentNode->removeChild($spanNode);