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.
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 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
I have created 3 simple functions that will probably need to be optimized, but they do their job, they are part of a bigger class that I use to format code, dates, values, etc…:
to use it simply call:
here is the code (still in beta, but so far haven’t had many issues with it)