Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
hanselman.com/blog/… – Chase Florell Jul 14 '11 at 1:51
How about Python's BeautifulSoup? It's got a prettify() function. – Kit Jul 14 '11 at 4:35
up vote 6 down vote accepted

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.

share|improve this answer
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 Jul 13 '11 at 23:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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