3v4l.org

run code in 500+ PHP versions simultaneously
<?php $allowed = array("img", "p","style"); $text='<img src="image.gif" onerror="myFunction()" style="background:red" onclick="myFunction()"> <p>A function is triggered if an error occurs when loading the image. The function shows an alert box with a text. In this example we refer to an image that does not exist, therefore the onerror event occurs.</p> <div>a sample div with a <span>span in it</span></div> <p>a sample para with a <span>span in it</span></p> <script> function myFunction() { alert(\'The image could not be loaded.\'); } </script>'; $doc = new DOMDocument(); $doc->loadHTML($text); $xp = new DOMXPath($doc); $find = '//body//*[not(name() = "'.implode ('" or name() = "', $allowed ). '")]'; echo "XPath = ".$find.PHP_EOL; $toRemove = $xp->evaluate($find); print_r($toRemove); foreach ( $toRemove as $remove ) { $remove->parentNode->removeChild($remove); } // recreate HTML $outHTML = ""; foreach ( $doc->getElementsByTagName("body")[0]->childNodes as $tag ) { $outHTML.= $doc->saveHTML($tag); } echo $outHTML;
Output for git.master, git.master_jit, rfc.property-hooks
XPath = //body//*[not(name() = "img" or name() = "p" or name() = "style")] DOMNodeList Object ( [length] => 4 ) <img src="image.gif" onerror="myFunction()" style="background:red" onclick="myFunction()"><p>A function is triggered if an error occurs when loading the image. The function shows an alert box with a text. In this example we refer to an image that does not exist, therefore the onerror event occurs.</p> <p>a sample para with a </p>

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
42.08 ms | 1491 KiB | 4 Q