Código PHP:
function removeUnsafeAttributesAndGivenTags($input, $validTags = '')
{
$regex = '#\s*<(/?\w+)\s+(?:on\w+\s*=\s*(["\'\s])?.+?
\(\1?.+?\1?\);?\1?|style=["\'].+?["\'])\s*>#is';
return preg_replace($regex, '<${1}>',strip_tags($input, $validTags));
}
A la función la uso así:
Código PHP:
$descripcion = removeUnsafeAttributesAndGivenTags($descripcion, "<span><br /><font><p><a><strong><em><u><strike><ol><li><ul><div>");
Pero la función elimina algunos atributos que necesito como: style. También me borra el br.
Este es el texto de entrada, tiene todos tags HTML permitidos:
Código HTML:
<span style="font-family: Comic Sans MS">a<br /> <strong>a</strong><br /> <em>a<br /> </em><u>a<br /> </u><strike>a</strike></span> <div style="text-align: left"><span style="font-family: Comic Sans MS">a<br /> </span></div> <div style="text-align: center"><span style="font-family: Comic Sans MS">a<br /> </span></div> <div style="text-align: right"><span style="font-family: Comic Sans MS">a<br /> </span></div> <div style="text-align: justify"><span style="font-family: Comic Sans MS">a<br /> </span></div> <ol> <li><span style="font-family: Comic Sans MS">a</span></li> </ol> <ul> <li>a<br type="_moz" /> <ul> <li>a</li> </ul> </li> </ul> <font color="#ff0000">a<br /> </font><span style="background-color: #ff0000">a<br /> <a href="http://a">a</a><br /> </span><br type="_moz" />
Y así después aplicar la función:
Código HTML:
<span>a <strong>a</strong> <em>a </em><u>a </u><strike>a</strike></span><div><span>a </span></div><div><span>a </span></div><div><span>a </span></div><div><span>a </span></div> <ol> <li><span>a</span></li> </ol> <ul> <li>a <ul> <li>a</li> </ul> </li> </ul> <font color="#ff0000">a </font><span>a <a href="http://a">a</a> </span>
Gracias.