Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 518
ianyoung Avatar asked Jun 22 '10 17:06

ianyoung


People also ask

How do I minify PHP HTML output?

Minification can be done by removing unnecessary details and eliminating excessive whitespaces, newlines, comments, etc. However, minification reduces the readability of the code. Minification can reduce file size upto 70%. PHP is used to transfer files from development to production environment.

Can you minify HTML?

To minify JS, CSS and HTML files, comments and extra spaces need to be removed, as well as crunch variable names so as to minimize code and reduce file size. The minified file version provides the same functionality while reducing the bandwidth of network requests.

How do you minify a project in HTML?

The easiest way to minify your HTML, CSS and JavaScript in WordPress is to use a plugin. This allows you to optimize your WordPress site files for decreased page load times automatically with a few clicks of a button. There are a lot of plugins out there that will do the job but I will briefly mention a few standouts.

Is it worth to Minify HTML?

Note that if your HTML is being dynamically generated then minifying the HTML takes extra CPU/time, but it is still worth it. Minification does not save bandwidth if you're GZipping since GZip will compress all spaces/newlines including any required whitespace.


1 Answers

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.

like image 98
Peter Anselmo Avatar answered Oct 14 '22 23:10

Peter Anselmo