I need to use PHP to remove HTML tags that either have an opening with no closing or a closing with no opening. For example, I would want <div><span></div> to convert to <div></div> and I would want <div></span></div> to also convert to <div></div>.

There are several reasons that I do not want to do this. They are:1.) This method is capable of catching when a tag is not properly closed, but it throws the falling warning.DOMDocument::loadHTML(): Unexpected end tag2.) The HTML it generates is intended to be on a separate page. It decks the variable out with the <html></html> tags, and throws <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> in for good measure.

How can I do what I want without doing something I don't want at the same time? I suppose I could always just use the string length function to hack my way around this, but I would like to avoid doing so if possible.