PHPでTidy HTMLの整形
PHPのTidyを使ってHTMLを整形
PHP
// ソース
$src = file_get_contents('http://php.o0o0.jp');
$tidy = new tidy;
// オプション
$config = array(
'doctype' => 'auto',
'indent' => true,
'wrap' => 200
);
// パース
$tidy->parseString($src, $config, 'utf8');
$tidy->cleanRepair();
// 整形後ソース
echo $tidy;
// エラー
echo $tidy->errorBuffer;
HTML5がエラー。
line 34 column 1 - Error: <header> is not recognized!
line 34 column 1 - Warning: discarding unexpected <header>
line 37 column 1 - Error: <nav> is not recognized!
line 37 column 1 - Warning: discarding unexpected <nav>
line 44 column 1 - Warning: discarding unexpected </nav>
line 46 column 1 - Warning: discarding unexpected </header>
line 51 column 1 - Error: <article> is not recognized!
line 51 column 1 - Warning: discarding unexpected <article>
line 52 column 1 - Error: <header> is not recognized!
line 52 column 1 - Warning: discarding unexpected <header>
line 58 column 1 - Warning: discarding unexpected </header>
line 74 column 1 - Error: <section> is not recognized!
line 74 column 1 - Warning: discarding unexpected <section>
line 76 column 354 - Warning: discarding unexpected </section>
line 79 column 1 - Error: <footer> is not recognized!
HTML5はPHP5.5時点で未対応。HTML5はWindows版を利用する。