9

First thing: I know that there is interface to W3C validator: http://pear.php.net/package/Services_W3C_HTMLValidator/ But I don't know if I can install it on cheap hosting server. I don't think so.

I need validator for my seo tools within my Content Managment System so it must be pretty much portable.

I would love to use W3C but only if it would be portable. I can also use Curl for this but it won't be elegant solution.

The best one I found so far is: http://phosphorusandlime.blogspot.com/2007/09/php-html-validator-class.html

Is there any validator comparable to W3C but portable (only PHP that does not depend on custom packages)?

bpeterson76
  • 12,918
  • 5
  • 49
  • 82
Kaminari
  • 1,387
  • 3
  • 17
  • 32
  • 3
    Why can't you install it on a "cheap hosting server"? – BoltClock Feb 17 '11 at 14:47
  • Well I loked closely to this script and it seems that it's only php and does not require custom PHP packages. – Kaminari Feb 17 '11 at 15:19
  • possible duplicate of [How to validate HTML/CSS in Localhost](http://stackoverflow.com/questions/4358348/how-to-validate-html-css-in-localhost) – Gordon Feb 17 '11 at 15:27
  • possible duplicate of [Which function in PHP validate if the string is valid HTML](http://stackoverflow.com/questions/3167074/which-function-in-php-validate-if-the-string-is-valid-html) – Gordon Feb 17 '11 at 15:28
  • possible duplicate of [Fixing malformed HTML in PHP](http://stackoverflow.com/questions/404377/fixing-malformed-html-in-php) – Gordon Feb 17 '11 at 15:29
  • more http://stackoverflow.com/search?q=validate+html+document+[php] – Gordon Feb 17 '11 at 15:29

2 Answers2

9

If you want to validate (X)HTML documents, you can use PHP's native DOM extension:

Example from Manual:

$dom = new DOMDocument;
$dom->load('book.xml'); // see docs for load, loadXml, loadHtml and loadHtmlFile
if ($dom->validate()) {
    echo "This document is valid!\n";
}

If you want the individual errors, fetch them with libxml_get_errors()

Richard
  • 4,341
  • 5
  • 35
  • 55
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • I think I tried DOMDocument::validate but had problems with it. I forget what these problems were exactly - it was a while ago now. So I set up my PHP code to access the W3C validator (just file_get_contents with the URL of the validation result page) and it worked, but nowadays for some reason the W3C server is sending a 403 error back to my PHP script. Strange - the validator is still accessible through my web browser. – Stewart Oct 26 '13 at 18:56
  • @Stewart There is also a SOAP API to the W3 Validator nowadays: http://validator.w3.org/docs/api.html – Gordon Oct 26 '13 at 22:02
  • @Gordon - This is doing the same as the regular HTML output - working through my browser but giving a 403 error when I try it through PHP. – Stewart Oct 26 '13 at 22:46
  • 1
    Just discovered that it now requires a User-Agent header to be set. So I've now got it setting one and it seems to work now. – Stewart Oct 26 '13 at 23:18
1

I asked a similar question and you might check out some of the answers there.

In summary, I would recommend either running the HTML through tidy on the host or writing a short script to validate through W3C remotely. Personally, I don't like the tidy option because it reformats your code and I hate how it puts <p> tags on every line.

Here's a link to tidy and here's a link to the various W3C validation tools.

One thing to keep in mind is that HTML validation doesn't work with server-side code; it only works after your PHP is evaluated. This means that you'd need to run your code through the host's PHP interpreter and then 'pipe' it to either the tidy utility or the remote validation service. That command would look something like:

$ php myscript.php | tidy #options go here

Personally, I eventually chose to forgo the headache and simply render the page, copy the source and validate via direct input on the W3C validation utility. There are only so many times you need to validate a page anyway and automating it seemed more trouble than it's worth.

Good luck.

Community
  • 1
  • 1
Thomas
  • 5,736
  • 8
  • 44
  • 67
  • Yes I checked all similar questions before posting but There was nothing new. Of course I know that I must validate php output but I'm pretty sure that I can simply call it by protocol for example (http://mysite.com/script.php) in CURL or file_get_contents. I should work fast like localhost. – Kaminari Feb 17 '11 at 15:15