1

I am providing my users to create their own widgets on their profile pages.Widgets are created by HTML codes by user input.

But Sometimes, if user enter an invalid html code, widget field is corrupting. To avoid this situation, I must be sure the html code that user entered is valid.

My question is how to check a html code validation via PHP or Javascript.

Thanks in advance.

Tuğrul Mete
  • 11
  • 1
  • 2
  • possible duplicate of [HTML Validator in Javascript/PHP? ( not necessarily XML )](http://stackoverflow.com/questions/1336108/html-validator-in-javascript-php-not-necessarily-xml) – Widor May 16 '12 at 10:50

2 Answers2

3

You can use the tidy php extension;

There are a number of things that you can do with it, but for your case I think you could just use the repairString method to get the clean version of the user input and compare it with the initial version,

Something like this:

$tidy = new tidy();
$clean = $tidy->repairString($user_input);

If no changes were made during the repairString method; $user_input and $clean should be the same.

0x3h
  • 452
  • 9
  • 22
mishu
  • 5,347
  • 1
  • 21
  • 39
  • 1
    I've always found tidy to be really unreliable. Last time I tried to use it, it insisted on making changes to my (valid) markup that I really didn't want. I ended up having to perform a find and replace, then run tidy, then run find and replace again to work around the problem. – Quentin May 16 '12 at 10:52
  • @Quentin and that did not depend on the doctype? – mishu May 16 '12 at 10:54
  • No, the Doctype was immaterial. – Quentin May 16 '12 at 10:54
  • @Quentin in this case thanks for sharing; this might be very useful in the future – mishu May 16 '12 at 10:55
  • Thanks for your answer. Tidy just worked well. I'm now repairing all user inputs with tidy and then save them. – Tuğrul Mete May 16 '12 at 11:15
0

Take a look at this SO question / answers. The DOMDocument-Class may validate your HTML with PHP.

Community
  • 1
  • 1
Michael Seibt
  • 1,346
  • 9
  • 13