Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Minify HTML code?

My idea is to somehow minify HTML code in server-side, so client receive less bytes.

What do I mean with "minify"?

Not zipping. More like, for example, jQuery creators do with .min.js versions. In other words, I need to remove unnecessary white-spaces and new-lines, but I can't remove so much that presentation of HTML changes (for example remove white-space between actual words in paragraph).

Is there any tools that can do it? I know there is HtmlPurifier. Is it able to do it? Any other options?

P.S. Please don't offer regex'ies. I know that only Chuck Norris can parse HTML with them. =]

like image 498
daGrevis Avatar asked Apr 28 '11 08:04

daGrevis


2 Answers

A bit late but still... By using output_buffering it is as simple as that:

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();
like image 164
Savas Vedova Avatar answered Sep 23 '22 21:09

Savas Vedova


You could parse the HTML code into a DOM tree (which should keep content whitespace in the nodes), then serialise it back into HTML, without any prettifying spaces.

like image 39
Delan Azabani Avatar answered Sep 23 '22 21:09

Delan Azabani