©
This document uses PHP Chinese website manual Release
This simple example shows basic Tidy usage.
Example #1 Basic Tidy usage
<?php
ob_start ();
?>
<html>a html document</html>
<?php
$html = ob_get_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 ;
?>
[#1] ryz [numbers:three one six] at hotmail dot com [2015-04-22 20:33:03]
An easy-to-remember alternative for simplicity in a production environment, which will not tamper with your HTML:
<?php
$html = str_replace("><", ">\r\n<", $html);
?>
It might not indent your code, but it makes it a damn site easier to read.
[#2] gk at anuary dot com [2014-02-22 22:56:22]
If you are looking 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 procession is relatively resource demanding task, 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 back, because output will not align with the input.
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://github.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.
[#3] i dot c dot lovett at NOSPAM dot gmail dot com [2012-03-12 20:22:28]
Anyone trying to specify "indent: auto" as documented at http://tidy.sourceforge.net/docs/quickref.html#indent
<?php
$tidy_options = array('indent' => 'auto'); // WILL NOT WORK
$tidy_options = array('indent' => 2); // equivalent of auto
$tidy = new Tidy();
$tidy->parseString($html, $tidy_options);
?>