Validating an HTML5 Document in PHP Using Tidy

I am trying to clear an HTML string and create an HTML5 document using Tidy and PHP, however I am creating an HTML3.2 document. Apparently, I get an error Config: missing or malformed argument for option: doctype. I am working with PHP Version 5.5.35 with Centos 6 and Apache 2.2, and php_info()shows the following:

tidy

Tidy support    enabled
libTidy Release 14 June 2007
Extension Version   2.0 ($Id: e066a98a414c7f79f89f697c19c4336c61bc617b $)

Directive   Local Value Master Value
tidy.clean_output   no value    no value
tidy.default_config no value    no value

How to create an HTML5 document? Following is my attempt:

<?php
$html = <<<EOD
<p>Hello</p>
<div>
 <p data-customattribute="will be an error">bla</p>
 <p>bla</p>
</div>
<div>
 <p>Hi there!</p>
 <div>
  <p>Opps, a mistake</px>
 </div>
</div>
EOD;
$html="<!DOCTYPE HTML><html><head><title></title></head><body>$html</body></html>";

echo($html."\n\n");

    $config = array(
        'indent' => true,
        'indent-spaces' => 4,
        'doctype' => '<!DOCTYPE HTML>',
    );

$tidy = new tidy;
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();
print_r($tidy);

OUTPUT

<!DOCTYPE HTML><html><head><title></title></head><body><p>Hello</p>
<div>
 <p data-customattribute="will be an error">bla</p>
 <p>bla</p>
</div>
<div>
 <p>Hi there!</p>
 <div>
  <p>Opps, a mistake</px>
 </div>
</div></body></html>

tidy Object
(
    [errorBuffer] => Config: missing or malformed argument for option: doctype
line 9 column 21 - Warning: discarding unexpected </px>
line 3 column 2 - Warning: <p> proprietary attribute "data-customattribute"
    [value] => <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
    <head>
        <title></title>
    </head>
    <body>
        <p>
            Hello
        </p>
        <div>
            <p data-customattribute="will be an error">
                bla
            </p>
            <p>
                bla
            </p>
        </div>
        <div>
            <p>
                Hi there!
            </p>
            <div>
                <p>
                    Opps, a mistake
                </p>
            </div>
        </div>
    </body>
</html>
)
+4
source share
1 answer

Older versions of Tidy do not support HTML5 documents

The first version tidysupports HTML 5 in Sep 2015 , where the HTML Tidy Advocacy Community Group released the first version of tidy-html5.

, tidy, ableto html5.

php tidy-html5, , tidy-html5, .

README tidy-html5 github:

- API PHP, "buffio.h" "tidybuffio.h" ext/tidy/tidy.c.

- php php:

   sed -i 's/buffio.h/tidybuffio.h/' ext/tidy/*.c

( , php):

   ./configure --with-tidy=/usr/local
   make
   make test
   make install
+1

Source: https://habr.com/ru/post/1648153/


All Articles