X/Html Validator in PHP

2020-08-24 11:32发布

问题:

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)?

回答1:

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

  • DOMDocument::validate — Validates the document based on its DTD

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()



回答2:

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.