3v4l.org

run code in 500+ PHP versions simultaneously
<?php $allowed_tags = array("img", "p"); $allowed_attributes = array("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>'; function remove_nodes_and_attributes($doc, $node, $allowed_tags, $allowed_attributes) { $xpath = new DOMXPath($doc); foreach ($xpath->query('./*', $node) as $child) { if (!in_array($child->nodeName, $allowed_tags)) { $node->removeChild($child); continue; } $a = 0; while ($a < $child->attributes->length) { $attribute = $child->attributes->item($a)->name; if (!in_array($attribute, $allowed_attributes)) { $child->removeAttribute($attribute); // don't increment the pointer as the list will shift with the removal of the attribute } else { // allowed attribute, skip it $a++; } } // remove any children as necessary remove_nodes_and_attributes($doc, $child, $allowed_tags, $allowed_attributes); } } $doc = new DOMDocument(); $doc->loadHTML("<html>$text</html>", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $html = $doc->getElementsByTagName('html')[0]; remove_nodes_and_attributes($doc, $html, $allowed_tags, $allowed_attributes); echo substr($doc->saveHTML(), 6, -8);
Output for git.master, git.master_jit, rfc.property-hooks
<img style="background:red"> <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:
41.43 ms | 1552 KiB | 4 Q