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.

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 and that did not depend on the doctype?
No, the Doctype was immaterial.
@Quentin in this case thanks for sharing; this might be very useful in the future
Thanks for your answer. Tidy just worked well. I'm now repairing all user inputs with tidy and then save them.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.