Feb 03 2010
HTML fixer
STOP bad html inserted by your clients or by the users of your community!
This PHP class lets you clean and repair html code. Here is a quick list of the magic things it can do (it’s really good when you don’t have the possibility to install the Html Tidy module of PHP).
WHAT IT DOES:
- delete closed tags without their opening tag
- fix open tag without close, closing them automatically
- check bad nesting and fix them (if you have a bold inside a bold… or a paragrah that contains a table…)
- fix bad quotes in attributes (open quotes where missing…)
- merge different styles attributes in the same tag
- remove html comments
- remove empty tags and more bad tags
How does it works?
it’s a bit complex to explain, it analyzes char by char the html code, detecting nodes, watching inside each node to fix quotes, attributes, and more and finding their closing tags. Save every node found and it’s inner content in a matrix.
And then it reads the matrix to re-build the fixed html.
The matrix stores open tags, closed tags and content and lets count the errors.
Watch a demo of the HTML FIXER CLASS with debug.
Watch a demo of the HTML FIXER CLASS with a textarea to insert dirty html code.
Download the class and the example.
HISTORY
NEW. version 2.03
strips php code.
version 2.02
fixed a bug with non closing quotes.

February 7th, 2010 11:02 am
interesting, thanks for sharing.
February 17th, 2010 1:05 pm
Hi,
I’m having the following problem with your class:
Imagine that my string is:
$string = “Etiam non massa sit amet lacus ultrices pulvinar id ultrices tellus. Proin ornare ante vel nibh adipiscing.”
When I do a $string=substr($string,0,120) my $string contains:
“Etiam non massa sit amet lacus ultrices pulvinar id ultrices tellus. Proin ornare ante <span style="color: rgb(1"
When I try to fix this with your class it goes to an infinite loop. Any sugestion?
February 21st, 2010 10:42 pm
Thank you very much, I’ve fixed it. The problem was due to the non closing quote. I’ve fixed it and now with your example works. Download the new version (2.02) at the same address.
March 4th, 2010 6:23 am
Nella tua demo online se scrivo semplicemente: ?>
da il seguente warning che sarebbe meglio nascondere
Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 0 in /web/htdocs/www.barattalo.it/home/examples/htmlfixer.class.php on line 363
saluti da chicago
March 4th, 2010 6:24 am
scusa intendevo <?
March 4th, 2010 9:57 am
Thanks, fixed. Now it strips also php code.