PHP code example of webignition / html-document-type

1. Go to this page and download the library: Download webignition/html-document-type library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webignition / html-document-type example snippets




use webignition\HtmlDocumentType\Factory;
use webignition\HtmlDocumentType\HtmlDocumentType;
use webignition\HtmlDocumentType\HtmlDocumentTypeInterface;

$html5DocType = new HtmlDocumentType();
echo $html5DocType;
// <!DOCTYPE html>

$html401StrictDocType = new HtmlDocumentType(
    '-//W3C//DTD HTML 4.01//EN', 
    'http://www.w3.org/TR/html4/strict.dtd',
    HtmlDocumentTypeInterface::PUBLIC_SYSTEM_KEYWORD_SYSTEM
);
echo html401StrictDocType;
// <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">



use webignition\HtmlDocumentType\HtmlDocumentTypeInterface;
use webignition\HtmlDocumentType\Parser\Parser;
use webignition\HtmlDocumentType\Parser\ParserInterface;

$parser = new Parser();

$html5DocTypeParts = $parser->parse('<!DOCTYPE html>');
var_dump($html5DocTypeParts);
// array(3) {
//     'public-system' => NULL
//     'fpi' => NULL
//     'uri' => NULL
// }

$html401StrictDocTypeParts = $parser->parse('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">');
var_dump($html401StrictDocTypeParts);
// array(3) {
//   'public-system' => string(6) "PUBLIC"
//   'fpi' => string(25) "-//W3C//DTD HTML 4.01//EN"
//   'uri' => string(37) "http://www.w3.org/TR/html4/strict.dtd"
// }



use webignition\HtmlDocumentType\Extractor;

$html5DocTypeString = Extractor::extract('<!DOCTYPE html><html>...</html>');
echo $html5DocTypeString;
// <!DOCTYPE html>


use webignition\HtmlDocumentType\Factory;

// Create from a known doctype string
$html5DocumentType = Factory::createFromDocTypeString('<!DOCTYPE html>');
echo $html5DocType;
// <!DOCTYPE html>


// Create from a HTML document
$html5DocumentType = Factory::createFromHtmlDocument('<!DOCTYPE html><html>...</html>');
echo $html5DocType;
// <!DOCTYPE html>


use webignition\HtmlDocumentType\Factory;
use webignition\HtmlDocumentType\Validator;

$validator = new Validator();

$html5DocumentType = Factory::createFromDocTypeString('<!DOCTYPE html>');
$validator->isValid($html5DocumentType);
// true

$html401StrictDocumentType = Factory::createFromDocTypeString('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">');
$validator->isValid($html401StrictDocumentType);
// true

$invalidDocumentType = new HtmlDocumentType('invalid FPI');
$validator->isValid($invalidDocumentType);
// false

// If the uri doesn't match what is expected for the fpi, the doctype is considered invalid
$laxHtml401StrictDocumentType = Factory::createFromDocTypeString('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "foo">');
$validator->isValid($laxHtml401StrictDocumentType);
// false

// You can relax the strictness of validity. This is useful in cases where a doctype string uses the uri of a draft version of a HTML spec (the doctype string was once valid but is no longer)
$validator->setMode(Validator::MODE_IGNORE_FPI_URI_VALIDITY);
$validator->isValid($laxHtml401StrictDocumentType);
// true

// Relaxing validity strictness has no impact on invalid fpi values
$validator->setMode(Validator::MODE_IGNORE_FPI_URI_VALIDITY);
$validator->isValid($invalidDocumentType);
// false