- var_dump: documentation ( source)
- strip_tags: documentation ( source)
- implode: documentation ( source)
- explode: documentation ( source)
- trim: documentation ( source)
- in_array: documentation ( source)
- array_diff: documentation ( source)
<?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;
}