Skip to content
PHP client for the Validator.nu / checker.html5.org API
Branch: master
Clone or download
Latest commit feebdfc Feb 21, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
library/HtmlValidator Change from bool to options array when validating URLs Feb 21, 2017
tests Change from bool to options array when validating URLs Feb 21, 2017
.gitignore Added phpunit.xml to gitignore Jun 18, 2014
.travis.yml Test on latest PHP version Feb 21, 2017
LICENSE Update copyright year Aug 7, 2016
README.md Add badges to readme Feb 21, 2017
composer.json
composer.lock Use Guzzle 6.2 Aug 7, 2016
phpunit.xml.dist Change backend from validator.nu to checker.html5.org Feb 21, 2017
phpunit.xml.travis Change backend from validator.nu to checker.html5.org Feb 21, 2017

README.md

html-validator

PHP client for the checker.html5.org API. Can be configured to use a self-hosted version of the API.

Latest Stable VersionPHP VersionLicenseBuild Status

Version >= 2.0.0 requires PHP >= 5.6.
Version <= 1.1.0 supports PHP <= 5.6, but won't be maintained anymore.

Usage

<?php
$document = file_get_contents('my-page.html');

$validator = new HtmlValidator\Validator();
$result = $validator->validateDocument($document);

$result->hasErrors();   // true / false
$result->hasWarnings(); // true / false

$result->getErrors();   // array(HtmlValidator\Message)

echo $result;           // Prints all messages in human-readable format
echo $result->toHTML(); // Prints all messages HTML-formatted

Installing

To include html-validator in your project, add it to your composer.json file:

{
    "require": {
        "rexxars/html-validator": "^2.1.0"
    }
}

Example

Document to be validated (validate-me.html):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>Invalid HTML4!</title>
</head>
<body>
    <p>This document is not a proper, well-formed HTML4 document!</p>
    <p>It contains fatal flaws, like:</p>
    <ul>
        <li><div> tags which are not closed</li>
        <li>span-tags which are never opened are attempted closed </span></li>
    </ul>
</body>
</html>

Using the validator:

<?php
$document = file_get_contents('validate-me.html');

$validator = new HtmlValidator\Validator();
$validator->setParser(HtmlValidator\Validator::PARSER_HTML4);
$result = $validator->validateDocument($document);

echo $result;

Output:

info: HTML4-specific tokenization errors are enabled.


error: End tag “li” seen, but there were open elements.
From line 10, column 44; to line 10, column 48
not closed</li>


error: Unclosed element “div”.
From line 10, column 13; to line 10, column 17
      <li><div> tags

error: Stray end tag “span”.
From line 11, column 67; to line 11, column 73
ed closed </span></li>

Validating a URL

Since 1.1.0 you can validate URLs as well:

<?php
$validator = new HtmlValidator\Validator();
$validator->setParser(HtmlValidator\Validator::PARSER_HTML5);
$result = $validator->validateUrl($url);

echo $result;

Note that if you want to check pages that return status codes that are not in the 2xx-range (like a 404-page), you need to pass a checkErrorPages option:

$validator = new HtmlValidator\Validator();
$validator->setParser(HtmlValidator\Validator::PARSER_HTML5);
$result = $validator->validateUrl($url, ['checkErrorPages' => true]);

echo $result;

Using a self-hosted version of the API

Check out validator.nu for instructions on setting up the service. Once set up, you can configure the validator to use a different host:

<?php
$validator = new HtmlValidator\Validator('http://self-hosted-validator.domain.com');

License

MIT licensed. See LICENSE for full terms.

You can’t perform that action at this time.