Skip to content Skip to sidebar Skip to footer

Php Using Domxpath To Strip Tags And Remove Nodes

I am trying to work with DOMDocument but I am encountering some problems. I have a string like this: Some Content to keep

Solution 1:

No, I wouldn't recommend regex, I strongly recommend build on what you have right now with the use of this beautiful HTML Parser. You could use ->replaceChild in this case:

$dom = new DOMDocument;
$dom->loadHTML($getVal);
$xPath = new DOMXPath($dom);

$spans = $xPath->query('//span');
foreach ($spansas$span) {
    $class = $xPath->evaluate('string(./@class)', $span);
    if(strpos($class, 'ice-ins') !== false || $class == '') {
        $span->parentNode->removeChild($span);
    } elseif(strpos($class, 'ice-del') !== false) {
        $span->parentNode->replaceChild(new DOMText($span->nodeValue), $span);
    }
}

$newString = $dom->saveHTML();

Solution 2:

More generic solution to delete any HTML tag from a DOM tree use this;

$dom = new DOMDocument;
$dom->loadHTML($getVal);
$xPath = new DOMXPath($dom);

$tagName = $xPath->query('//table'); //use what you want like div, span etc.foreach ($tagNameas$t) {
    $t->parentNode->removeChild($span);
}

$newString = $dom->saveHTML();

Example html:

<html><head></head><body><table><tr><td>Hello world</td></tr></table></body></html>

Output after process;

<html><head></head><body></body></html>

Post a Comment for "Php Using Domxpath To Strip Tags And Remove Nodes"