SunshinePHP 2019 CFP Started

tidy::cleanRepair

tidy_clean_repair

(PHP 5, PHP 7, PECL tidy >= 0.5.2)

tidy::cleanRepair -- tidy_clean_repairExecute configured cleanup and repair operations on parsed markup

说明

面向对象风格

bool tidy::cleanRepair ( void )

过程化风格

bool tidy_clean_repair ( tidy $object )

This function cleans and repairs the given tidy object.

参数

object

The Tidy 对象。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

Example #1 tidy::cleanrepair() example

<?php
$html 
'<p>test</I>';

$tidy tidy_parse_string($html);
$tidy->cleanRepair();

echo 
$tidy;
?>

以上例程会输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
<p>test</p>
</body>
</html>

参见

add a note add a note

User Contributed Notes 2 notes

up
1
steven at nevvix dot com
8 days ago
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.

<?php
$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);
up
1
steven at nevvix dot com
9 days ago
<?php
/**
* UTF-8 HTML5-compatible Tidy
*
* @param string $html
* @param array $config
* @param string $encoding
* @link http://tidy.sourceforge.net/docs/quickref.html
*/
function tidy_html5($html, array $config = [], $encoding = 'utf8') {
    if (!
extension_loaded('tidy')) {
        throw new \
Exception("Tidy extension is missing!");
        return;
    }
   
$config += [
       
'clean'       => TRUE,
       
'doctype'     => 'omit',
       
'indent'      => 2, // auto
       
'output-html' => TRUE,
       
'tidy-mark'   => FALSE,
       
'wrap'        => 0,
       
// HTML5 tags
       
'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',
    ];
   
$html = tidy_parse_string($html, $config, $encoding);
   
tidy_clean_repair($html);
    return
'<!DOCTYPE html>' . PHP_EOL . $html;
}

$html = '<z/><p><a href="#">Link</a></p><p>Second para</p>';
echo
tidy_html5($html);

Output:
<!
DOCTYPE html>
<
html>
<
head>
  <
title></title>
</
head>
<
body>
  <
p><a href="#">Link</a></p>
  <
p>Second para</p>
</
body>
</
html>
To Top