fork download
  1. <?php
  2.  
  3. $html = <<<DATA
  4. <html>
  5. <body>
  6. <div id="r1">
  7.  <a href="link">Link</a>
  8. </div>
  9. <div id="r2">
  10.  <a href="link">Link 2015-09-12</a>
  11. </div>
  12. </body>
  13. </html>
  14. DATA;
  15.  
  16. $dom = new DOMDocument;
  17. $dom->loadHTML($html);
  18. $myVariable = "2015-09-12";
  19.  
  20. $xpath = new DOMXPath($dom);
  21. $divs = $xpath->query("//div[contains(.,'$myVariable')]");
  22.  
  23. foreach($divs as $div) {
  24. $div->parentNode->removeChild($div);
  25. }
  26.  
  27. echo $dom->saveHTML();
Success #stdin #stdout 0.03s 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>
<div id="r1">
 <a href="link">Link</a>
</div>

</body></html>