Based on the strip_tags documentation, the second parameter takes the allowable tags. However in my case, I want to do the reverse. Say I’ll accept the tags the script_tags normally (default) accept, but strip only the <script> tag. Any possible way for this?
I don’t mean somebody to code it for me, but rather an input of possible ways on how to achieve this (if possible) is greatly appreciated.
EDIT
To use the HTML Purifier
HTML.ForbiddenElementsconfig directive, it seems you would do something like:http://htmlpurifier.org/docs
HTML.ForbiddenElementsshould be set to anarray. What I don’t know is what form thearraymembers should take:Or:
Or… Something else?
I think it’s the first form, without delimiters;
HTML.AllowedElementsuses a form of configuration string somewhat common to TinyMCE’svalid elementssyntax:So my guess is it’s just the term, and no attributes should be provided (since you’re banning the element… although there is a
HTML.ForbiddenAttributes, too). But that’s a guess.I’ll add this note from the
HTML.ForbiddenAttributesdocs, as well:Blacklisting is just not as "robust" as whitelisting, but you may have your reasons. Just beware and be careful.
Without testing, I’m not sure what to tell you. I’ll keep looking for an answer, but I will likely go to bed first. It is very late.
:)Although I think you really should use HTML Purifier and utilize it’s
HTML.ForbiddenElementsconfiguration directive, I think a reasonable alternative if you really, really want to usestrip_tags()is to derive a whitelist from the blacklist. In other words, remove what you don’t want and then use what’s left.For instance:
Then run it:
http://codepad.org/LV8ckRjd
So if you pass in what you don’t want to allow, it will give you back the HTML5 element list in an
arrayform that you can then feed intostrip_tags()after joining it into a string:Caveat Emptor
Now, I’ve kind’ve hacked this together and I know there are some issues I haven’t thought out yet. For instance, from the
strip_tags()man page for the$allowable_tagsargument:It’s late and for some reason I can’t quite figure out what this means for this approach. So I’ll have to think about that tomorrow. I also compiled the HTML element list in the function’s
$html5element from this MDN documentation page. Sharp-eyed reader’s might notice all of the tags are in this form:I’m not sure how this will effect the outcome, whether I need to take into account variations in the use of a shorttag
<tagName/>and some of the, ahem, odder variations. And, of course, there are more tags out there.So it’s probably not production ready. But you get the idea.