I’ve worked on a similar problem last weekend and found this extremely usefull function:
function strip_tags_attributes($string,$allowtags=NULL,$allowattributes=NULL){
$string = strip_tags($string,$allowtags);
if (!is_null($allowattributes)) {
if(!is_array($allowattributes))
$allowattributes = explode(",",$allowattributes);
if(is_array($allowattributes))
$allowattributes = implode(")(?<!",$allowattributes);
if (strlen($allowattributes) > 0)
$allowattributes = "(?<!".$allowattributes.")";
$string = preg_replace_callback("/<[^>]*>/i",create_function(
'$matches',
'return preg_replace("/ [^ =]*'.$allowattributes.'"[^"]*"|'[^']*')/i", "", $matches[0]);'
),$string);
}
return $string;
}
You can apply it on a piece of content like this:
strip_tags_attributes($content, '<p><a><img><object><param><embed>','href,src,allowfullscreen,type');
The tags and attributes in this example are allowed, everything else is stripped. This example also leaves embedded video, but because the width and height attributes are stripped you can define those in the CSS so the object/embed fits in your widget. Or just disallow object/embed.
Where and how you should apply this function in your case I haven’t figured out yet. Let me know, I should probably use this myself.