The configuration reference for Tidy has moved to https://api.html-tidy.org/
(PHP 5, PHP 7, PHP 8, PECL tidy >= 0.5.2)
tidy::parseString -- tidy_parse_string — Analisa um documento armazenado em uma string
Estilo orientado a objetos
$string
, array|string|null $config
= null
, ?string $encoding
= null
): boolEstilo procedural
$string
, array|string|null $config
= null
, ?string $encoding
= null
): tidy|falseAnalisa um documento armazenado em uma string.
string
Os dados a serem analisados.
config
O parâmetro de configuração config
pode ser passado como um
array ou como uma string. Se uma string for passada, ela será interpretada como o
nome do arquivo de configuração; caso contrário, será interpretada como as
próprias opções.
Para uma explicação sobre cada opção, visite » http://api.html-tidy.org/#quick-reference.
encoding
O parâmetro encoding
define a codificação para
documentos de entrada/saída. Os valores possíveis para codificação são:
ascii
, latin0
, latin1
,
raw
, utf8
, iso2022
,
mac
, win1252
, ibm858
,
utf16
, utf16le
, utf16be
,
big5
e shiftjis
.
tidy::parseString() retorna true
em caso de sucesso.
tidy_parse_string() retorna uma nova instância de tidy
em caso de sucesso.
Tanto o método quanto a função retornam false
em caso de falha.
Versão | Descrição |
---|---|
8.0.0 |
config e encoding agora podem ser anulados.
|
Exemplo #1 Exemplo de tidy::parseString()
<?php
ob_start();
?>
<html>
<head>
<title>teste</title>
</head>
<body>
<p>erro<br>outra linha</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;
?>
O exemplo acima produzirá:
<!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> teste </title> </head> <body> <p> erro<br /> outra linha </p> </body> </html>
The configuration reference for Tidy has moved to https://api.html-tidy.org/
<?php
/**
* Simpler version without pretty print config options.
*/
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;
}
$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ç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>
<?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') {
$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>