How can I remove DOM element tags but leave their contents?

后端 未结 3 819
深忆病人
深忆病人 2021-01-19 14:20

I have PHP code which removes all nodes that have at least one attribute. Here is my code:


    

These

3条回答
  •  野的像风
    2021-01-19 15:05

    This will remove all tags that have class and style attributes, so it's not a bullet proof:

    
        

    These line shall stay

    Remove this one

    But keep this

    and this
    DATA; $dom = new DOMDOcument(); $dom->loadHTML($data, LIBXML_HTML_NOIMPLIED); $dom->removeChild($dom->doctype); $xpath = new DOMXPath($dom); $lines_to_be_removed = $xpath->query("//*[count(@class)>0 or count(@style)>0]"); foreach ($lines_to_be_removed as $line) { $line->parentNode->removeChild($line); } // just to check echo $dom->saveHTML(); ?>

    Note this line:

     $lines_to_be_removed = $xpath->query("//*[count(@class)>0] or count(@style)>0]");
    

提交回复
热议问题