Validates HTML using

2.3.0 2020-06-30 18:25 UTC

This package is auto-updated.

Last update: 2022-09-29 01:44:34 UTC


PHP client for the 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.


$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


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

    "require": {
        "rexxars/html-validator": "^2.2.0"


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

    <title>Invalid HTML4!</title>
    <p>This document is not a proper, well-formed HTML4 document!</p>
    <p>It contains fatal flaws, like:</p>
        <li><div> tags which are not closed</li>
        <li>span-tags which are never opened are attempted closed </span></li>

Using the validator:

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

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

echo $result;


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:

$validator = new HtmlValidator\Validator();
$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();
$result = $validator->validateUrl($url, ['checkErrorPages' => true]);

echo $result;

Using a self-hosted version of the API

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

$validator = new HtmlValidator\Validator('');


MIT licensed. See LICENSE for full terms.