HTML XPATH - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of HTML XPATH.php

  1. <?php
  2. $content = '<div class="keep-me">Keep this div</div><div class="remove-me" id="test">Remove this div</div>';
  3. $badClasses = array('');
  4.  
  5. $dom = new DOMDocument;
  6. libxml_use_internal_errors(true);
  7. $dom->loadHTML($content);
  8. libxml_clear_errors();
  9. $xPath = new DOMXpath($dom);
  10.  
  11. foreach($badClasses as $badClass){
  12.     $domNodeList = $xPath->query('//div[@class="remove-me"]/@id');
  13.  
  14.     $domElemsToRemove = ''; // container of deleted elements
  15.     foreach ( $domNodeList as $domElement ) {
  16.         $domElemsToRemove .= $dom->saveHTML($domElement); // concat them
  17.         $domElement->parentNode->removeChild($domElement); // then remove
  18.     }
  19.  
  20. }
  21.  
  22. $content = $dom->saveHTML();
  23. echo htmlentities($domElemsToRemove);
  24. ?>
File Description
  • HTML XPATH
  • PHP Code
  • 17 Dec-2018
  • 714 Bytes
You can Share it: