简体   繁体   中英

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
<span class="ice-cts-1 ice-del" data-changedata="" data-cid="5" data-time="1414514760583" data-userid="1" data-username="Site Administrator" undefined="Site Administrator">
     This content should remain, but span around it should be stripped
</span> 
     Keep this content too
<span>
     <span class="ice-cts-1 ice-ins" data-changedata="" data-cid="2" data-time="1414512278297" data-userid="1" data-username="Site Administrator" undefined="Site Administrator">
         This whole node should be deleted
     </span>
</span>

What I want to do is, if the span has a class like ice-del keep the inner content but remove the span tags. If it has ice-ins , remove the whole node.

If it is just an empty span <span></span> remove it as well. This is the code I have:

//this get the above mentioned string
$getVal = $array['body'][0][$a];
$dom = new DOMDocument;
$dom->loadHTML($getVal );
$xPath = new DOMXPath($dom);
$delNodes = $xPath->query('//span[@class="ice-cts-1 ice-del"]');
$insNodes = $xPath->query('//span[@class="ice-cts-1 ice-ins"]');

foreach($insNodes as $span){
    //reject these changes, so remove whole node
    $span->parentNode->removeChild($span);
}

foreach($delNodes as $span){
    //accept these changes, so just strip out the tags but keep the content
}

$newString = $dom->saveHTML();

So, my code works to delete the entire span node, but how do I take a node and strip out it tags but keep its content?

Also, how would I just delete and empty span? I'm sure I could do this using regex or replace but I kind of want to do this using the dom.

thanks

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 ($spans as $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();

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 ($tagName as $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>

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