fork download
  1. <?php
  2.  
  3. $html = <<<EOF
  4. <html>
  5. <p>some random text</p>
  6.  
  7. <div class="greybackground_desktop" style="background-color:#EFEFEF;">
  8. <!-- /49527960/CSF_Article_Middle -->
  9. <div style="padding-bottom:10px; padding-top: 10px; text-align:center;" id='div-gpt-ad-1441883689230-0'>
  10. <script type='text/javascript'>
  11. googletag.cmd.push(function() { googletag.display('div-gpt-ad-1441883689230-0'); });
  12. </script>
  13. </div>
  14. </div>
  15.  
  16. <p>some more text</p>
  17.  
  18. <div><p>example of content that will be incorrectly removed</p></div>
  19.  
  20. <p>Text that follows</p>
  21. </html>
  22. EOF;
  23.  
  24. $dom = new DOMDocument();
  25. $dom->loadHTML($html);
  26.  
  27. $xpath = new DOMXpath($dom);
  28.  
  29. foreach ($xpath->query("//div[@class='greybackground_desktop']") as $div)
  30. $div->parentNode->removeChild($div);
  31.  
  32. echo $dom->saveHTML();
  33.  
  34. ?>
Success #stdin #stdout 0.02s 52480KB
stdin
Standard input is empty
stdout
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://w...content-available-to-author-only...3.org/TR/REC-html40/loose.dtd">
<html><body><p>some random text</p>



<p>some more text</p>

<div><p>example of content that will be incorrectly removed</p></div>

<p>Text that follows</p>
</body></html>