1

I would like to have a PHP function to check if a URL returns valid HTML or NOT, and returns true or false.

Something like:

if (validate_page("/somefile.html")) { echo "This page validated!!"; }

I found TWINE but it doesn't just give me true or false. Also I got an error running it on my system. http://twineproject.sourceforge.net/

I found this offline tool that looked promising. http://htmlhelp.com/tools/validator/offline/

Also I found this thread that talks about a gem, but it sounds problematic. How do I validate XHTML with nokogiri?

1
  • The thread talking about a gem is about Ruby (a gem is a package), not PHP. Commented Sep 15, 2010 at 5:27

5 Answers 5

2

Tidy?

Validate: http://us.php.net/manual/en/function.tidy-diagnose.php
Repair: http://us.php.net/manual/en/tidy.repairstring.php

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

1 Comment

this looks promising, I'll try it out!
1

You can use W3C's validator API. There's a PHP library available through PEAR (click here) which uses said API.

You can also install the validator on your local server (instructions here), though you might not have sufficient permissions to do so if you are using shared hosting.

1 Comment

This is what I ended up using, thanks for this answer. Sorry for belated response
1

You could also try DOMDocument->validate() if you are using PHP 5 and if the document contains a DTD.

http://www.php.net/manual/en/domdocument.validate.php

Comments

0

xhtml hast to be valid xml - if you only want to check that, you could easily use simplexml, but if you also want to check for correct elements/attributes this won't help you (in that case, NullUserExceptions hint to W3C's validator API would be the best solution to choose).

Comments

0
libxml_use_internal_errors ( true );
$doc = new DOMDocument;
$doc -> loadHTMLFile ( $file ); // load the file you want validated
var_dump ( libxml_get_errors () );

1 Comment

Thanks for this code sample! I got this working, but it gave me a false positive on one of my test cases. Also it only works on static fules (not php templates). But good enough for now, thanks!

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.