I just installed HTML tidy using the following command line:

sudo apt-get install php5-tidy

yet I have no idea how to use it because I thought it is an auto process that can do things by itself after all.

I found this little piece of code instructing how to use, but not sure where to put the code!!

The code is:

 $config = array(
            'indent'         => true,
            'output-xhtml'   => true,
            'show-body-only' => true,
            'wrap'           => 0);

    $tidy = new tidy;
    $tidy->parseString($html_txt, $config, 'utf8');

    echo $tidy;

I found this info on this link: Use HTML Tidy with PHP5 under Ubuntu/Debian

My purpose is to tidy both HTML and PHP files on my Ngninx server. I tried some Nginx Modules such as Mod_strip, but didn't work for me...!!

any idea how to use above code and where??

1 Answer 1


That code looks to be PHP code for configuring & running Tidy from within PHP directly. That doesn't sound like what you're looking for. What you want is to be able to run Tidy on .html files. Using Tidy in this fashion has nothing to do with Nginx. You can simply run Tidy on any .html files like so:

$ tidy -m some.html

This will tidy up a single HTML file, some.html, writing the modifications in place. This is the tip of the iceberg in what you can do with Tidy. You can give it a series of files, you can include your configuration options in a config file and tell tidy where to read this file and report any errors to another file like so:

$ tidy -config cfg.tidy -f errors.txt -m some.html

To run Tidy on a directory tree that contains .html files you'll have to construct your own script(s) to do this.

What about PHP files?

To my knowledge Tidy doesn't handle pure .php files, it will deal with PHP that's embedded in PHP tags within an HTML files, in a limited way, but that's it.

You can however make use of other code beautifiers such as PHP_Beautifier. There were others listed in this Life Hacker articled titled: Web Developer Tools: Code Beautifier and Formatter.

More info about the code snippet?

If you're still interested in the snippet of code that you included in your question, there are fairly good examples of how to make use of Tidy from within a PHP script here in this articled titled: Tidying up HTML code with Tidy PHP extension.


  • amazing!! Thanks@slm. That was what I'm looking for. Cheers! Commented Jun 8, 2014 at 2:54
  • @Digitalsite - you're welcome, thanks for the Q.
    – slm
    Commented Jun 8, 2014 at 2:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .