Function to remove specified html tags from string I recently wrote this function that, when supplied a string and an array of element types, will remove those element types from the string.

As you could imagine this is a very very simple function, but I wanted to get opinions on speed, functionality, and any other comments anyone can come up with that can improve this function.

&lt;?

    $str = &lt;&lt;&lt;STRING
        &lt;b&gt;Chocolate&lt;/b&gt; is very &lt;div class="vanilla"&gt;Chocolate&lt;/div&gt;!
        &lt; b&gt;England&lt;/b&gt; is very &lt;div class="skyscraper"&gt;City&lt;/div&gt;!
        &lt;b&gt;England&lt;/b&gt; is my &lt;div&gt;city&lt;/div&gt;
        &lt;b&gt;England&lt;/b&gt; is my &lt;div&gt;city&lt;/div&gt;
        &lt;b&gt;England&lt;/b&gt; is my &lt;span&gt;city&lt;/span&gt;
        &lt; b&gt;Name&lt;/b&gt;
        &lt;div class="myclass"&gt;&lt;/div&gt;
        &lt;span&gt;
STRING;

    echo htmlentities(removeTags($str, array("b", "div", "span")));

    function removeTags($str, $tags) {

        $tagsString = "";
        foreach($tags as $key =&gt; $v) { 
            $tagsString .= $key == count($tags)-1 ? $v : "{$v}|"; 
        }

        $patterns = array("/(&lt;\s*\b({$tagsString})\b[^&gt;]*&gt;)/i", "/(&lt;\/\s*\b({$tagsString})\b\s*&gt;)/i");
        $output = preg_replace($patterns, "", $str);

        return $output;

    }

?&gt;
Alright!  You made me do it.  I'm posting an answer :)

Parsing arbitrary HTML via regular expressions is nearly impossible.  If you were using this on HTML code that you control yourself, it is certainly possible, because you can make sure that all of the edge cases never happen.  However, if you are going to set this loose on arbitrary HTML from the internet, you will find that it fails to work in a number of cases.  Your use case is a little simpler because you are just trying to remove the tags themselves (and not their contents), but even still it can be surprisingly tricky.  You probably need a straight-up HTML parser.  There are a bajillion of them for PHP.  Other than the easy ones I mentioned in my initial comment, here are some more variations that I think will completely sink your current code.  I have actually seen this first one in use on the intertubes: it is technically valid HTML:

&lt;a href="#" class="tooltip" data-contents="&lt;br&gt;Hi&lt;br&gt;"&gt;Hover me&lt;/a&gt;


Another good one: your new regular expression allows for more flexibility in the tag contents at the expense of tag specificity.  In other words, I'm pretty sure that if you use your script to try to remove &lt;b&gt; tags, you will also end up removing &lt;br&gt; tags.

These, and the examples in my initial comment, are some exceptions I came up with just off the top of my head.  In contrast, the internet cumulatively has a bajillion hours experience writing hard-to-parse HTML strings.  Sure, your code might even work 90% of the time, but if you stick with regular expressions it will definitely fail on a regular basis.  It's up to you to decide if the failure rate is low enough.

Especially important: If you are trying to use this as a security tool then definitely don't.  It is nowhere near reliable enough.  Sorry.  It's not that your algorithm isn't well thought out.  It's just that HTML is a full dialect that needs an actual parser: regular expressions just aren't meant for this kind of thing.  It's like trying to build an actual house out of toothpicks.

See this similar question/answer:

Regex to remove inline javascript from string