3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = "<input type=\"text\">\n<img src=\"\"><a href=\"\"><button type=\"button\"></button><div id=\"some\"></div><p></p>"; var_dump(removeTags($str, array('img'))); /** * Strip only certain tags in the given HTML string * @return String */ function removeTags($html, $tags){ $existing_tags = getAllTagNames($html); $allowable_tags = '<'.implode('><', array_diff($existing_tags, $tags)).'>'; return strip_tags($html, $allowable_tags); } /** * Get a list of tag names in the provided HTML string * @return Array */ function getAllTagNames($html){ $tags = array(); $part = explode("<", $html); foreach($part as $tag){ $chunk = explode(" ", $tag); if(empty($chunk[0]) || $chunk[0][0] == "/") continue; $tag = trim($chunk[0], " >"); if(!in_array($tag, $tags)) $tags[] = $tag; } return $tags; }

preferences:
57.41 ms | 402 KiB | 5 Q