How to minimize HTML code?

My idea is to somehow minimize the server side HTML, so the client receives fewer bytes.

What do I mean by "minify"?

Does not fasten. Moreover, for example, jQuery creators use .min .js versions. In other words, I need to remove unnecessary spaces and newlines, but I cannot remove the representation of HTML changes so much (for example, remove the spaces between the actual words in the paragraph).

Are there any tools that can do this? I know there is an HtmlPurifier . Can it do this? Any other options?

PS Please do not offer regular expressions. I know that only Chuck Norris can parse HTML with them. =]

+8
source share
5 answers

You can parse the HTML in the DOM tree (which should contain content spaces in the nodes) and then serialize it back to HTML without any highlight spaces.

+3
source

A bit late, but still ... Using output_buffering, it's just as simple:

function compress($string) { // Remove html comments $string = preg_replace('/<!--.*-->/', '', $string); // Merge multiple spaces into one space $string = preg_replace('/\s+/', ' ', $string); // Remove space between tags. Skip the following if // you want as it will also remove the space // between <span>Hello</span> <span>World</span>. return preg_replace('/>\s+</', '><', $string); } ob_start('compress'); // Here goes your html. ob_end_flush(); 
+9
source

Are there any tools that can do this?

Yes, here is a tool that you can include in the build process or work in the network cache layer: http://code.google.com/p/htmlcompressor/

Or, if you are looking for the HTML minimization tool that you embed, try: http://www.willpeavy.com/minifier/

+2
source

Are there any tools that can do this?

You can use the CodVerter Online Editor for web development to compress mixed HTML.
The compressor has been tested several times for reliability and accuracy.
(Full disclosure: I am one of the developers).

enter image description here

+2
source

You can use the Pretty Diff tool: http://prettydiff.com/?m=minify&html It also minimizes any CSS and JavaScript in the HTML code, and it minimizes in a regressive manner so as not to interfere with the future decoration of HTML back in a readable form.

0
source

All Articles