Command line html formatter that works on linux (or a way to do this in PHP)?

html, php

Solution

You are probably looking for Tidy. It cleans and formats XML and HTML. There is also a PHP extension, but you would probably need to buffer your output and pass it through there.

Edit: A code example:

ob_start();
// output your html
$output = ob_get_flush();

// 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;

I didn't test this, but it should work.

Problem

Does anyone know of a linux-compatible command line html formatter? You know, something where I could pass it a file that looks like: ``` <html> <body> <p> hi </p> </body> </html> ``` And it gives me: ``` <html> <body> <p> hi </p> </body> </html> ``` I'm using PHP to generate this html, so if there is some handy way to do this via php that I'm missing?

Original source