Pour l'utiliser, il faut, selon votre situation, soit simplement l'activer (si vous utilisez WampServer, par exemple), soit l'installer.
Voici un exemple simple d'utilisation :
<?php
ob_start();
?>
<html>
<head>
<title>test</title>
</head>
<body>
<p>error</i>
</body>
</html>
<?php
$buffer = ob_get_clean();
$tidy = tidy_repair_string($buffer);
echo $tidy;
?>
Dans notre exemple, le code HTML est erroné. En effet, notre balise "<p>" n'est pas fermée et le mot "error" est entouré d'une balise fermante "i" jamais ouverte. La fonction "tidy_repair_string" va simplement nettoyer le code et nous renvoyer un HTML tout propre.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>test</title>
</head>
<body>
<p>error</p>
</body>
</html>
La fonction susmentionnée a donc fermé la balise "p", supprimé la balise "i" et ajouté un "doctype".
Si vous souhaitez uniquement parser le "body" d'un HTML, il suffit d'appeler l'option "show-body-only" :
$str = “<p>error.”
tidy_repair_string($str, array('show-body-only' => true), "utf8");
echo $str;
Le code ci-dessus retournera le code HTML suivant :
<p>error.</p>
Pour plus d'informations :