Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pretty-print HTML via PHP without validation?

Tags:

html

php

tidy

I'd like to automatically pretty-print (indentation, mostly) the HTML output that my PHP scripts generate. I've been messing with Tidy, but have found that in its efforts to validate and clean my code, Tidy is changing way too much. I know Tidy's intentions are good but I'm really just looking for an HTML beautifier. Is there a simpler library out there that can run in PHP and just do the pretty-printing? Or, is there a way to configure Tidy to skip all the validation stuff and just beautify?

like image 673
brianjcohen Avatar asked Dec 24 '10 18:12

brianjcohen


1 Answers

The behaviour that you've observed when using Tidy is a result of the underlying use of DOM API. Instead of manipulating the provided source code, DOM API will reconstruct the whole source, thus making fixes along the way.

I've written Dindent, which is a library that uses Regex. It does not do anything beyond adding the indentation and removing whitespaces. However, I advise against using this implementation beyond development purposes.

like image 129
Gajus Avatar answered Oct 06 '22 21:10

Gajus