views:

73

answers:

3

I need to find a PHP based HTML (as in WC3-Like) Validator that can look for invalid HTML or XHTML. I've searched Google a little, but was curious if anyone has used one they particularly liked.

I have the HTML in a string:

$html = "<html><head>.....</body></html>";

And I would like to be able to test the page, and have it return the errors. (Not echo/print anything)

I've seen:
-http://www.bermi.org/xhtml_validator
-http://twineproject.sourceforge.net/doc/phphtml.html

The background for this is that I'd like to have a function/class that I run on every page, check if the file has been modified since the last access date (or something similar to that), and if it hasn't, run the validator so I am immediately notified of invalid HTML while coding.

+2  A: 

There's no need to reinvent the wheel on this one. There's already a PEAR library that interfaces with the W3C HTML Validator API. They're willing to do the work for you, so why not let them? :)

Robert Elwell
Pretty cool, but you have to rely on thier webservice. This means you must be connected to a public internet. very neat though.
Byron Whitlock
This is an definitely an option.
Chacha102
+1  A: 

While it isn't strictly PHP, (it is a executable) one i really like is w3c's HTML tidy. it will show what is wrong with the HTML, and fix it if you want it to. It also beautifies HTML so it doesn't look like a mess. runs from the command line and is easy to integrate into php.

check it out. http://www.w3.org/People/Raggett/tidy/

Byron Whitlock
Note that PHP has a Tidy module - http://php.net/tidy.
ceejayoz
A: 

If you can't use Tidy (sometimes hosting service do not activate this php module), you can use this PHP class: http://www.barattalo.it/html-fixer/

Pons