Tuğrul Mete
Tuğrul Mete

Reputation: 11

How to check if the HTML code is valid via PHP or Javascript

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.

Upvotes: 1

Views: 5003

Answers (2)

mishu
mishu

Reputation: 5397

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.

Upvotes: 3

Michael Seibt
Michael Seibt

Reputation: 1346

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

Upvotes: 0

Related Questions