Skip to content

Instantly share code, notes, and snippets.

@stemar
Last active September 15, 2019 04:19
Show Gist options
  • Select an option

  • Save stemar/5794bd48576ae6adf405c6f21ce7dab0 to your computer and use it in GitHub Desktop.

Select an option

Save stemar/5794bd48576ae6adf405c6f21ce7dab0 to your computer and use it in GitHub Desktop.
UTF-8 HTML5-compatible Tidy output
<?php
function tidy_html5($html, array $config = [], $encoding = 'utf8') {
$config += [
'doctype' => '<!DOCTYPE html>',
'drop-empty-elements' => 0,
'new-blocklevel-tags' => 'article aside audio bdi canvas details dialog figcaption figure footer header hgroup main menu menuitem nav section source summary template track video',
'new-empty-tags' => 'command embed keygen source track wbr',
'new-inline-tags' => 'audio command datalist embed keygen mark menuitem meter output progress source time video wbr',
'tidy-mark' => 0,
];
$html = tidy_parse_string($html, $config, $encoding); // doctype not inserted
tidy_clean_repair($html); // doctype inserted
return $html;
}
<?php
$html = '</z><p><a href="#">Link</a></p><p><img src="logo.png"/>Seçond para</p><i class="fa"></i><p></p><wbr>';
echo tidy_html5($html, ['indent'=>2, 'indent-spaces'=>4]);
/*
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<p><a href="#">Link</a></p>
<p><img src="logo.png">Seçond para</p><i class="fa"></i>
<p></p><wbr>
</body>
</html>
*/
@stemar

stemar commented Aug 7, 2018

Copy link
Copy Markdown
Author

Use LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED as option in DOMDocument::loadHTML() to load the HTML without the <!DOCTYPE> and <html> tags so that tidy_html5() can insert them.

$html = '</z><p><a href="#">Link</a></p><p>Second para</p>';
$doc = new DOMDocument();
$doc->preserveWhiteSpace = FALSE;
$doc->formatOutput = TRUE;
$doc->loadHTML($html, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
echo tidy_html5($html);
<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <p><a href="#">Link</a></p>
  <p>Second para</p>
</body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment