views:

632

answers:

3
+1  Q: 

Minify HTML/PHP

I'm using gzip to compress my html/php files along with js/css/etc. This reduces the payload quite nicely but I also want to 'minify' my markup of both .html and .php pages. Ideally I'd like to control this from a .htaccess file (where I also do the gzipping) rather than the having to include php to each file.

I'd like the output to be like that of http://google.com or http://www.w3-edge.com/wordpress-plugins/w3-total-cache/ and http://css-tricks.com (both produced by W3 Total Cache plugin for WordPress).

Can anyone recommend a good way to do this.

+5  A: 

Looking at the examples, minifying the HTML output does almost nothing. Think about it: Minifying is great for Javascript which uses many repeating variable and function names, as one of the main things minifying does is shorten them and all references to them.

HTML markup, on the other hand, has no concept of variables or functions. Most of the weight of the page is from actual markup and content. This cannot be minified. Even form variables need to be left alone as they must have their original values to be processed correctly by the server.

Gzipping will already compress the whitespace very efficiently. In HTML, this is really all that can be done.

Also, minifying PHP doesn't apply, because even though it has variables and functions, it is never sent to the client. Shortening the names has no performance benefit for something compiled on the server.

If you are determined to minify your html, check out the source code for the WordPress plugin that does it. That's the beauty of open source. However, I suspect the gains will be negligible as compared to Gzipping.

Peter Anselmo
I didn't address minifying PHP, as it doesn't apply. PHP code is never sent to the client.
Peter Anselmo
@Peter Anselmo: You should add that comment to your answer.
R. Bemrose
I think he meant the HTML output of PHP pages.
Jarrod
@R. Bemose - Good idea, It's been merged with the answer.
Peter Anselmo
"minifying the HTML output does almost nothing". I generally agree with the "don't minify unless you really need to" sentiment, however, google certainly does it for a reason (albeit one that probably doesn't apply to 99% of projects). There's a couple of reasons I'd seriously consider HTML minification for my projects. (1) iPhone only caches objects smaller than something like 30k, and compression doesn't help get around that limit (2) if you don't have to worry about whitespace and comments being sent to the client, you can use more than you might otherwise -- we indent our markup heavily.
Frank Farmer
A: 

Peter Anselmo has confused minification for obfuscation. In code obfuscation the code is minified and variables are renamed to shortest length arbitrary names. Minification is merely the practice of reducing code size, such as white space removal, without altering the values, names, or syntax of code.

Peter Anselmo is also wrong that minifying markup results in an insignificant savings. This page, for instance, shows a savings of 18.31% and it was pretty tidy to begin with. Clearly, he has never tested his opinion before he put it out there. You can see the cost savings yourself using the Pretty Diff tool at http://prettydiff.com/

You can attempt to reverse engineer minification engine used by Pretty Diff to execute from PHP. That code and accompanied documentation can be found at: prettydiff.com/markupmin.js

austincheney
Normally obfuscation is more than that! He did say that minifying gains would be insignficant *compared to gzipping*. From a quick trial that's 18% vs 75%. Now gzipping the minified version will save a little more - another 1-2% - but you got most of the way there with the gzip alone.
Rup
Yes, you completely took my phrase out of context which was concerning minifying in addition to gzipping. Your tool pretty diff does not evaluate this scenario, it only evaluates uncompresseed code vs minified ucompressed code.Also, many minifiers (including the YUI compressor, Closure Compilier, and Packer) DO shorten variable names. Perhaps I should have been more clear about minification vs obfuscation for the benefit of OP, but I still don't see any need to edit my post.
Peter Anselmo
I should have been a little clearer but yes I did mean just simple minification and stripping of whitespace rather than obfuscation. A good method I have found is to use output buffering with obstart() which can simply be called in the header and used in combination with some regular expressions to strip anything unnecessary out of the markup before sending it to the browser.With regards to the PHP pages, yes I just meant the markup of those pages.The prettydiff tool is pretty useful. Thanks for the tip!
Ian
A: 

I want to see (code) content (HTML) WITH whitespace in a CMS (Drupal/WordPress) before it gets minimized can I remove minify prior to posting content?

Lenny Zenith
Before it gets minimised? The minification occurs server-side before it is sent to the browser so this is only possible if you turn whatever it is you're using to minify off.
Ian