Using content management systems to build dynamic sites invariably results in ugly HTML. Indent levels are often wrong, white space litters the page, and a host of other tiny annoyances plague anyone attempting to read the source code for debugging or educational purposes.

Is there a way to automatically strip white space, autoindent, and otherwise beautify the output of a PHP or other dynamic script that spits out HTML?

I'm interested in any automated server-side solution that will improve the legibility of dynamically generated HTML, as well as solutions that strip all white space for compression purposes. If it matters, I normally run Apache or nginx.

It's safe to assume that I'm caching the output, so I won't be running whatever HTML beautification witchcraft you suggest on every page view (and taking the associated performance hit each time). As such, I'm more interested in ease of setup and reliable results than I am in performance.


1 Answer 1


Check out HTML Tidy. There are a lot of different bindings for different languages, so you should be able to find one that works for you.

Edit: If you're looking for something actively maintained, check out HTMLawed.

  • Is it still in active development? I discounted it because it hasn't been updated for over 2 years, which suggests to me that it might not support HTML 5 elements, and prove hard to maintain in future.
    – Nick
    Commented Jul 13, 2011 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.