tidy::parseString

tidy_parse_string

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

tidy::parseString -- tidy_parse_string Analiza un documento HTML contenido en una string

Descripción

Estilo orientado a objetos

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

Estilo por procedimientos

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

Analiza un documento contenido en una string.

Parámetros

string

Los datos a analizar.

config

La configuración config puede ser pasada en forma de array o de string. Si una string es pasada, es interpretada como el nombre del fichero de configuración, y si no, es interpretada como las opciones mismas.

Para una explicación sobre cada opción, véase » http://api.html-tidy.org/#quick-reference.

encoding

El parámetro encoding configura la codificación para los documentos de entrada y salida. Los valores posibles son ascii, latin0, latin1, raw, utf8, iso2022, mac, win1252, ibm858, utf16, utf16le, utf16be, big5 y shiftjis.

Valores devueltos

tidy::parseString() devuelve true en caso de éxito. tidy_parse_string() devuelve una nueva instancia de tidy en caso de éxito. Ambos, el método y la función devuelven false en caso de fallo.

Historial de cambios

Versión Descripción
8.0.0 config y encoding son ahora nullable.

Ejemplos

Ejemplo #1 Ejemplo con tidy::parseString()

<?php
ob_start
();
?>

<html>
<head>
<title>test</title>
</head>
<body>
<p>error<br />otra línea</p>
</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;
?>

El resultado del ejemplo sería:

<!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 />
   otra línea
  </p>
 </body>
</html>

Ver también

add a note

User Contributed Notes 3 notes

up
1
steven at nevvix dot com
6 years ago
<?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&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>
up
0
gbirch at tech-tamer dot com
10 months ago
The configuration reference for Tidy has moved to https://api.html-tidy.org/
up
0
steven at nevvix dot com
6 years 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') {
$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