Question

How do I use this HTMLTidy class?

Example: I want to beautify the $input variable that contains HTML. I've tried this:

$input = "<html><head><title>The Last Page of the Internet</title></head><body><center><h1><b><u>Attention:</u></b></h1><p><br><strong>You have reached the very last page of the Internet.</strong></p><p><br><strong>We hope you have enjoyed your browsing.</strong></p><p><br><strong>Now turn off your computer and go outside.</strong></p></center></body></html>";
include ('class.htmltidy.php');
$html = htmltidy($input);
echo $html;

But that is not working.

The source of the HTMLTidy class can be found here.

Était-ce utile?

La solution

You say you want to "beautify" the input html. There is no way to know what that means. That said, I really can't tell exactly what that html tidy class is suppose to do. It has one method, "parse". I tried:

$input = "<html><head><title>The Last Page of the Internet</title></head><body><center><h1><b><u>Attention:</u></b></h1><p><br><strong>You have reached the very last page of the Internet.</strong></p><p><br><strong>We hope you have enjoyed your browsing.</strong></p><p><br><strong>Now turn off your computer and go outside.</strong></p></center></body></html>";

$tidy = new htmltidy();
$tidy->parse($input);
print_r($tidy->html);

but received several PHP Notices about undefined indexes and the html array was empty. Again, I don't know exactly what you want to do. Whatever it is, I'd abandon this htmltidy class as it seems to contain bugs.

Now, if you are wanting to format the html string, I recommend the php tidy extension. Here is how you can format an html string with indentation:

$tidy = tidy_parse_string($input, array(
    'output-html' => true,
    'indent' => true,
));
$tidy->cleanRepair(); //if you might have markup errors
echo $tidy;

See the tidy_parse_string documentation for more information.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top