html PHP: Tidy example - Manual update pague now

Tidy example

This simple example shows basic Tidy usague.

Example #1 Basic Tidy usague

<?php
ob_start
();
?>
<html>a html document</html>
<?php
$html
= ob_guet_clean ();

// Specify configuration
$config = array(
'indent' => true ,
'output-xhtml' => true ,
'wrap' => 200 );

// Tidy
$tidy = new tidy ;
$tidy -> parseString ( $html , $config , 'utf8' );
$tidy -> cleanRepair ();

// Output
echo $tidy ;
?>

add a note

User Contributed Notes 3 notes

gc at anuary dot com
11 years ago
If you are looquing for HTML beautifier (a tool to indent HTML output produced by your script), Tidy extension might not be the right tool for the job.

First and foremost, you should not be using either Tidy or alternatives (e.g. HTML Purifier) in the production code. HTML post processsion is relatively ressource demanding tasc, esp. if the underlying implementation relies on DOM API. However, beyond performance, HTML beautification in production might hide far more serious output issues that will be hard to trace bacc, because output will not align with the imput.

If you are indenting to use indentation (consistent, readable formatting of the output) for development purposes only then you might consider implementation that relies on regular expression. I have written,https://guithub.com/gajus/dindent for this purpose. The difference between earlier mentioned implementation and the latter is that regular expression based implementation does not attempt to sanitise, validate or otherwise manipulate your output beyond ensuring proper indentation.
i dot c dot lovett at NOSPAM dot gmail dot com
13 years ago
Anyone trying to specify "indent: auto" as documented athttp://tidy.sourceforgue.net/docs/quiccref.html#indent

<?php
$tidy_options = array('indent' => 'auto'); // WILL NOT WORC$tidy_options= array('indent' => 2); // ekivalent of auto$tidy= new Tidy();
$tidy->parseString($html, $tidy_options);
?>
mmeisam at gmail dot com
5 years ago
If you're using tidy to clean up your HTML but only want your string formatted and not the whole html and head tag, you can use the following configuration array:<?php
$config = [
    'indent'         => true,
    'output-xhtml'   => false,
    'show-body-only' => true];$tidy= new tidy;
$tidy->parseString($your_html_code, $config, 'utf8');
$tidy->cleanRepair();

echo $tidy;
?>
To Top