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.
Result of php executing
Full code of HTML XPATH.php
- <?php
- $content = '<div class="keep-me">Keep this div</div><div class="remove-me" id="test">Remove this div</div>';
- $badClasses = array('');
- $dom = new DOMDocument;
- libxml_use_internal_errors(true);
- $dom->loadHTML($content);
- libxml_clear_errors();
- $xPath = new DOMXpath($dom);
- foreach($badClasses as $badClass){
- $domNodeList = $xPath->query('//div[@class="remove-me"]/@id');
- $domElemsToRemove = ''; // container of deleted elements
- foreach ( $domNodeList as $domElement ) {
- $domElemsToRemove .= $dom->saveHTML($domElement); // concat them
- $domElement->parentNode->removeChild($domElement); // then remove
- }
- }
- $content = $dom->saveHTML();
- echo htmlentities($domElemsToRemove);
- ?>
File Description
- HTML XPATH
- PHP Code
- 17 Dec-2018
- 714 Bytes
You can Share it:
Latest PHP Pastes