Tech Note: A PHP Function to Strip Specific HTML Tags and Attributes
Every once in a while I come up with a bit of code that does something well enough I think it’s worth sharing, especially if it performs a function commonly used by lots of programmers, with a method that doesn’t already have hundreds of results in a Google search. And here’s one of those bits; maybe this will come in handy to another coder out there on the Internets, searching in vain for a good routine to parse tags and attributes.
For a long time I’ve been using a function to “sanitize” user input (e.g. comments and LGF Pages) to ensure no malicious code ever gets posted either on purpose or by accident. But I also allow certain HTML tags, and certain attributes for those tags.
The problem with the function I was using is that it employed regular expressions to parse the HTML, and that’s just a big freaking headache — inflexible and fragile. If you search a site like Stack Overflow for info on parsing HTML with regular expressions you’ll see tons of comments telling you “DO NOT DO THIS.” But the code worked well enough for a long time — until I started needing to allow HTML5 “data” attributes inside some tags.
Data attributes typically look like this:
The word “data” is followed by a hyphen and then a variable name for the type of data involved. And that’s where my old attribute parsing function started to become a real hassle to use, because it was unable to deal with that variable part of the name easily, and I needed to add every type of data attribute I used to a list as they came up.
So today I came up with a vastly improved function that uses PHP’s DOMDocument library to actually build a Document Object Model out of the HTML code, then find the tags and attributes the right way instead of the bad old regex way, and remove everything except the tags and attributes I want to leave in place.
The new approach lets me use a regular expression only to match all types of data attributes — not to to find those attributes inside a big pile of HTML code. The task of finding the tags and attributes is handled by the XPath syntax of PHP’s DOMDocument methods.
There are a couple of gotchas with using PHP’s DOMDocument library I should mention:
- If you're parsing a fragment of HTML code instead of an entire page, and it doesn't have a character encoding tag, DOMDocument will assume the text is encoded in ISO-8859 instead of the much preferred UTF-8. So this function uses the mb_convert_encoding function (line 7) to convert any Unicode characters into HTML entities before loading the code fragment, then uses html_entity_decode (line 32) to convert the entities back into characters when the parsing is finished.
- The second gotcha is that when you're parsing an HTML fragment, DOMDocument always adds a DOCTYPE, <html> and <body> tags to the fragment, and you can't disable this "feature." So after parsing, my function uses strip_tags a second time to remove those extra unneeded tags.
Oh yes, and one more thing; this is what the two arrays of tags and attributes look like for our comments; notice that the last item in the $commentAttributes array is a simple regular expression that matches any type of data attribute:
<?php $commentTags = array( 'b', 'i', 'a', 'strong', 'em', 'blockquote', 'div', 'p', 'br', 'strike', 'del', 'sup', 'sub', 'code', 'pre', 'span', 'img', 'button', ); $commentAttributes = array( 'href', 'rel', 'target', 'src', 'width', 'height', 'class', 'data-\S*' ); ?>
There are more sanitization measures in place when comments are posted, by the way; for example, images can only be embedded in comments if they’re uploaded and hosted at LGF, otherwise they’re transformed into links to the external images. This is to prevent anyone posting malicious images that contain code, porn, etc.