Strip HTML tags and its contents

后端 未结 2 665
忘掉有多难
忘掉有多难 2020-12-03 10:58

I\'m using DOM to parse string. I need function that strips span tags and its contents. For example, if I have:

This is some text that contains photo.


        
2条回答
  •  广开言路
    2020-12-03 11:55

    Try removing the spans directly from the DOM tree.

    $dom = new DOMDocument();
    $dom->loadHTML($string);
    $dom->preserveWhiteSpace = false;
    
    $elements = $dom->getElementsByTagName('span');
    while($span = $elements->item(0)) {       
       $span->parentNode->removeChild($span);
    }
    
    echo $dom->saveHTML();
    

提交回复
热议问题