2017 © Pedro Peláez
 

library html-validator

Validates HTML using Validator.nu

image

rexxars/html-validator

Validates HTML using Validator.nu

  • Wednesday, February 22, 2017
  • by rexxars
  • Repository
  • 3 Watchers
  • 9 Stars
  • 20,365 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 2 Open issues
  • 6 Versions
  • 32 % Grown

The README.md

html-validator

PHP client for the validator.nu API. Can be configured to use a self-hosted version of the API., (*1)

Latest Stable VersionPHP VersionLicenseBuild Status, (*2)

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

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:, (*4)

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

Example

Document to be validated (validate-me.html): ``` html , (*5)

Invalid HTML4!

This document is not a proper, well-formed HTML4 document!, (*6)

It contains fatal flaws, like:, (*7)

  • tags which are not closed
  • span-tags which are never opened are attempted closed

  • Using the validator: ```php <?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:, (*8)

    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:, (*9)

    <?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:, (*10)

    $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:, (*11)

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

    License

    MIT licensed. See LICENSE for full terms., (*12)

The Versions

22/02 2017

dev-master

9999999-dev

Validates HTML using Validator.nu

  Sources   Download

MIT

The Requires

 

The Development Requires

22/02 2017

2.1.0

2.1.0.0

Validates HTML using Validator.nu

  Sources   Download

MIT

The Requires

 

The Development Requires

07/08 2016

2.0.0

2.0.0.0

Validates HTML using Validator.nu

  Sources   Download

MIT

The Requires

 

The Development Requires

01/08 2015

1.1.0

1.1.0.0

Validates HTML using Validator.nu

  Sources   Download

MIT

The Requires

 

The Development Requires

24/11 2014

1.0.1

1.0.1.0

Validates HTML using Validator.nu

  Sources   Download

MIT

The Requires

 

The Development Requires

19/06 2014

1.0.0

1.0.0.0

Validates HTML using Validator.nu

  Sources   Download

MIT

The Requires

 

The Development Requires