tidy::parseString

tidy_parse_string

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

tidy::parseString -- tidy_parse_string解析存储在字符串中的文档

描述

面向对象风格

public tidy::parseString(string $string, array|string|null $config = null, ?string $encoding = null): bool

过程式风格

tidy_parse_string(string $string, array|string|null $config = null, ?string $encoding = null): tidy|false

解析存储在字符串中的文档。

参数

string

要解析的数据。

config

配置 config 可以以数组或字符串的形式传递。如果传递的是字符串,则将其解释为配置文件的名称,否则将其解释为选项本身。

有关每个选项的说明,请访问 » http://api.html-tidy.org/#quick-reference

encoding

参数 encoding 设置输入/输出文档的编码。编码的可能值为:asciilatin0latin1rawutf8iso2022macwin1252ibm858utf16utf16leutf16bebig5shiftjis

返回值

tidy::parseString() 成功时返回 truetidy_parse_string() 成功时返回一个新的 tidy 实例。方法和函数在失败时都返回 false

变更日志

版本 描述
8.0.0 configencoding 现在可为空。

示例

示例 #1 tidy::parseString() 示例

<?php
ob_start
();
?>

<html>
<head>
<title>test</title>
</head>
<body>
<p>error<br>another line</i>
</body>
</html>

<?php

$buffer
= ob_get_clean();
$config = array('indent' => TRUE,
'output-xhtml' => TRUE,
'wrap' => 200);

$tidy = tidy_parse_string($buffer, $config, 'UTF8');

$tidy->cleanRepair();
echo
$tidy;
?>

上面的示例将输出

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>
      test
    </title>
  </head>
  <body>
    <p>
      error<br />
      another line
    </p>
  </body>
</html>

参见

添加说明

用户贡献说明 3 个说明

steven at nevvix dot com
6 年前
<?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
tidy_clean_repair($html); // 插入 doctype
return $html;
}

$html = '</z><p><a href="#">Link</a></p><p><img src="logo.png"/>Seçond para</p><i class="fa"></i><p></p>';

echo
tidy_html5($html);

<!
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>
</
body>
</
html>

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>
</
body>
</
html>

echo
tidy_html5($html, ['indent'=>1], 'ascii');

<!
DOCTYPE html>
<
html>
<
head>
<
title></title>
</
head>
<
body>
<
p>
<
a href="#">Link</a>
</
p>
<
p>
<
img src="logo.png">Se&Atilde;&sect;ond para
</p><i class="fa"></i>
<
p></p>
</
body>
</
html>

echo
tidy_html5($html, ['show-body-only'=>1]);

<
p><a href="#">Link</a></p>
<
p><img src="logo.png">Seçond para</p>
<
i class="fa"></i>
<
p></p>
gbirch at tech-tamer dot com
2 个月前
Tidy 的配置参考已移至 https://api.html-tidy.org/
steven at nevvix dot com
6 年前
<?php
/**
* UTF-8 HTML5 兼容 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') {
$config += [
'clean' => TRUE,
'doctype' => 'omit',
'indent' => 2, // auto
'output-html' => TRUE,
'tidy-mark' => FALSE,
'wrap' => 0,
// HTML5 标签
'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