7

I was wondering whether there is class or something similar which I can include into my PHP pages to beautify the HTML output.

Such as putting new lines in after tags and correctly indenting so that my source code isn't only one line, I know that to the browser it doesn't matter but I wish to do this.

I have heard of http://www.php.net/manual/en/book.tidy.php but am not clear on what it does and how to implement it, i.e. I don't understand what the manual says about it.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
George Reith
  • 13,132
  • 18
  • 79
  • 148
  • 2
    You didn't understand the documentation for the PHP feature that does _exactly_ what you want, so you just ignored it and decided to ask for something else? Did you not try [the provided examples](http://www.php.net/manual/en/tidy.examples.basic.php)? – Lightness Races in Orbit Aug 18 '11 at 12:07
  • 1
    Why not just write beautiful HTML in the first place? If you're generating [any more than tiny snippets of structure] from within PHP code, you shouldn't be... Write templates, and you can write those however you like. – Lightness Races in Orbit Aug 18 '11 at 12:08
  • There's plenty of cases for this, although pretty source code just for the sake of it is probably just vanity - but you could fix broken source code, which is important if you have user provided HTML (CMS maybe). Or compressed output from the database you want to "beautify". I have to admit I don't understand the Tidy class from the examples on php.net either. – Wesley Murch Aug 18 '11 at 12:09
  • 1
    Besides, pretty source code in php templates (something you should do IMO) doesn't always add to up to pretty HTML source. – Wesley Murch Aug 18 '11 at 12:17
  • 1
    @Tomalak Geret'kal I did try the examples and saw no change in my code, I don't understanding the wording on there. I don't want to fill my php code with horrible tabs and newlines cluttering it up but I also don't have the time to write a templating system so I just need a resource that will capture my html output and display it neatly. – George Reith Aug 18 '11 at 12:48

3 Answers3

3

The Tidy extension is the way to go.

If you don't understand the documentation (OK, admittedly it's not very thorough), then the first results on Google for php tidy tutorials look very promising:

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Finally got this working, but it is removing my span tags from within my a tags which is standards compliant, can't find any documentation on why this happens do you know anything on this? I set it up as so: `ob_start( );` in my header and `$html = ob_get_clean(); $config = array('indent' => TRUE, 'output-xhtml' => TRUE, 'wrap' => 200); $tidy = tidy_parse_string($html, $config, 'UTF8'); echo $tidy;` in my footer – George Reith Aug 18 '11 at 13:13
  • nevermind the span tags where empty so it removed them, I just added a space inside them. – George Reith Aug 18 '11 at 13:25
  • Did you read the [config documentation](http://tidy.sourceforge.net/docs/quickref.html)? – Lightness Races in Orbit Aug 18 '11 at 13:33
  • Yes but I haven't seen anything to ignore removal of empty tags. – George Reith Aug 18 '11 at 13:35
  • @George: You completely ignored `drop-empty-paras`, then, which is explicitly about removal of empty tags. Now, granted, it says "paragraphs", but it may also have an undocumented effect here. You may also want to look at `merge-spans`. Please spend more time perusing the available options in future. – Lightness Races in Orbit Aug 18 '11 at 13:36
  • 1
    `drop-empty-paras` doesn't stop my spans being removed if empty, included in configuration array as such: `'drop-empty-paras' => FALSE` – George Reith Aug 18 '11 at 13:43
  • 1
    -1, Tidy is more trouble than what it's worth. It's not compatible with new HTML5 elements (you can fake it with some hard work) and it removes all empty tags (such as ``, ``, `
  • ` and so on. – Alix Axel Jun 16 '13 at 01:54