Feb 03 2010
HTML fixer
LAST UPDATE: 06/07/2010
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.05 date 06/07/2010
bug fixed on quotes by emmanuel (at) evobilis.com
version 2.04
added css style filter by Martin Vool
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.
April 14th, 2010 11:29 am
this class is not opening a tag, for the closed tags! Please let me know how we can do this?
April 19th, 2010 12:02 pm
Hello, i added a little feature to this. It enables to set the allowed style attributes (I dont want the user to screw up the website font face or encoding etc… by pasting from word or similar).
To add this feature i modifyed the code as follows:
mergeStyleAttributes functions end:
$check=explode(‘;’, $x);
$x=”;
foreach($check as $chk){
foreach($this->allowed_styles as $as){
if(stripos($chk, $as) !== False){
$x.=$chk.’;';
break;
}
}
}
if ($c>0) $s = str_replace(“##PUTITHERE##”,”style=\”".$x.”\”",$s);
return $s;
And added “public $allowed_styles;” to the class.
To use it:
$a = new HtmlFixer();
$a->allowed_styles=array(‘font-weight’,'width’,'height’,'align’,'valign’,…);
$str = $a->getFixedHtml(stripslashes($str));
Can be easely rewritten to restrict only listed options…
just put a ! in front of “stripos”
Maybe you should write a strip_tags similar functionality also? (so to be able to allow/deny certain html tags? I could help you.
April 27th, 2010 11:20 pm
Hi,
Great and useful script, was wondering, how to allow php to be included within the dirty html (but obviously ignored, so the php is not effected but the html is – so the php remains in the output), as im a beginner php user, and have a file containing both dirty html and php, extracting php and readding it is a tedious task!.
Looking forward to your reply.
Thanks
April 28th, 2010 2:38 pm
Thank you Martin Vool, I’ve added your code to the class!
April 28th, 2010 6:47 pm
Hi, admin, can you check:
http://www.barattalo.it/html-fixer/comment-page-1/#comment-1439
Any ideas?
Thanks
June 23rd, 2010 3:50 pm
Hi,
It seems you class doesn’t work for html code with missing quote as follow:
I checked your code, I found 2 possible issue:
1- in function “fixQuotes”: you forgot to init the variable $q to “\”"
…
private function fixQuotes($s) {
$q = “\”";
…
2- in function “fixTag”: you code will fix quote only for Tag that have a beginning quote
“if (stristr($ar[$i],”=”) && !stristr($ar[$i],”=\”")) $ar[$i] = $this->fixQuotes($ar[$i]);”
It hsould fix quote for any case:
…
if (stristr($ar[$i],”=”)) $ar[$i] = $this->fixQuotes($ar[$i]);
…
Ive tested it, it seems to work correctly. I let you check and amend your class.
Cheers & thanks again for your class.
July 9th, 2010 8:49 pm
Thanks for sharing this tool. Great work.
July 12th, 2010 2:39 pm
Great class, it really works very well. However I would argue some of your logic.
Example:
here we go!One more line
Your class will kill the em as your logic denies em outside a P, resulting in this code:
here we go!One more line
However, given the fact that code might come from eg. TinyMVE or another WYSIWYG my example is a valid one and should produce the following code if fixed at all:
here we go!One more line
Killing the doesnt really fix anything, it accually destroys the presentation of the page.
However, easy to prevent in your code however by just modifying the array for the p check. As I do not have had time to let your class sink into my brain yet, I havnt come up with my fix just disabled the fix. If I do, I will surely post it here!
July 12th, 2010 2:41 pm
That didn’t work, original post with the correct HTML here:
http://codepad.org/PGSn8vuG
July 14th, 2010 10:26 pm
I’ve understood your problem, but I think it’s not easy to be fixed. If you modify the class tell me and send me the new code, I will update the class and add your name to the class’ developer.
July 14th, 2010 10:59 pm
Thanks, I’ve modified the code.
July 14th, 2010 11:13 pm
Hi Darren, I think that is necessary to detect the php start and end tag and work in a different way for that code. Not so simple I think.
Let me know if you want to extend this class with that feature!
July 21st, 2010 2:35 pm
Thanks for sharing this class. Great work.
August 16th, 2010 1:24 pm
Very good. But I found problem with ordered and unordered lists. Example: Bad code with only opened tags “sometingother” it’s not working well, I got closing tags at end code instead of end every list item. See “sometingother”
August 16th, 2010 1:31 pm
Form deleted inserted html tags ….
August 23rd, 2010 10:58 pm
sorry if I’m wrong, but it’s has wrong quote at line number 100, but I help you to fix it.
Mine it’s working great now, if anyone want the working code you can download from
http://blog-walk.com/download <—- delete if you don't like it and mail me, I'll remove it from my site
regrads
August 25th, 2010 3:04 pm
I’ve also fixed it here. Bye!
August 25th, 2010 3:06 pm
Please, make the link to my site clickable, I’ve also fixed the quotes on line 100. Thank you.