PHP - 删除包含特定文本的父项
PHP - delete parent which contains specific text
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>
尝试
$spanNode->parentNode->removeChild($spanNode);
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
.
我的代码如下:
$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>
尝试
$spanNode->parentNode->removeChild($spanNode);