简体   繁体   中英

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>

How to remove just span which contains text Lorem ipsum . Everything after Lorem ipsum can be variable, thats why i need to search jsut by Lorem ipsum .

My code looks like:

$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
}

End result should looks like:

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

尝试

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM