tags:

views:

171

answers:

2

Folks does anyone knows of a PHP function to remove unmatched HTML tags from a string. for example<div> This is a string <b> with an unmatched bold tag </div>. If there isnt one then help me buld one, maybe I can have a function that counts the number of opening tags and matching closing tags. If they are not even then remove the first opening tag or if closing tags are more, it removes the last tag?

A: 

Without adhering to some sort of rule structure, this isn't very feasible. If you want to follow standards (that is, don't have a </b> break out of a containing block), you can do a lookahead with regex to verify that </b> is found before </div> is found.

http://www.regular-expressions.info/lookaround.html

dclowd9901
For the love of God. *DO NOT* use regular expressions to parse HTML!http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags
Vivin Paliath
@Vivin I wish people would stop citing that. Seriously. In this case, OP is trying to handle malformed HTML. A parser is not going to help there.
Matt
Indeed. I'm not entirely sure why one would believe this wouldn't work, or would somehow be a bad idea. And, Vivin, what exactly do you think your precious 'tidy' uses to clean up HTML? I guarantee it's not `str_replace()`.
dclowd9901
The reason I say that is because manipulating HTML (I should have said *manipulate* instead of *parse*) with regular expressions is not a robust solution. Sure, it *works* in some cases, but it's not something you want to rely on. It's fragile. I'm not sure what `str_replace()` has to do with this. I'm well aware that tidy doesn't use that. Sorry if I came off as rude. That wasn't my intention! :)
Vivin Paliath
+2  A: 

I don't believe there is a function. What you want to do is use something like tidy, which PHP supports (PHP Tidy). Tidy will clean up your HTML for you. Also, please don't get the idea to fix this using regular expressions! ;)

Here is a tutorial from Zend that talks about tidying up your HTML:

Vivin Paliath
Thanks Vivin you saved my day but this extension has very limited documentation in the PHP manual but the tutorial did it for me!
Freeman
No worries! I'm glad it was helpful!
Vivin Paliath