Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

php

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?

like image 476
ryeguy Avatar asked Sep 02 '09 19:09

ryeguy


2 Answers

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.

like image 166
mooware Avatar answered Nov 19 '22 07:11

mooware


Take a look at PHP's Tidy offering.

like image 34
Doug Hays Avatar answered Nov 19 '22 08:11

Doug Hays