Martin Thoma
Martin Thoma

Reputation: 136605

How can I automatically minify my webpages?

All of my .html and .php webpages should be minified. That means I would like to get all HTML-comments and more than one whitespace-character stripped.

Does an Apache-module exist for minification? (Or perhaps another method to automatically add a script directly before the output gets sent to the user?)

(I could add a function like the following, but if an Apache-module or another automatic solution existed, I could not forget to do so.)

<?
function sanitize_output($buffer)
{
    $search = array(
        '/\>[^\S ]+/s', //strip whitespaces after tags, except space
        '/[^\S ]+\</s', //strip whitespaces before tags, except space
        '/(\s)+/s'  // shorten multiple whitespace sequences
        );
    $replace = array(
        '>',
        '<',
        '\\1'
        );
  $buffer = preg_replace($search, $replace, $buffer);
    return $buffer;
}   
?>

Upvotes: 14

Views: 22951

Answers (1)

shrikeh
shrikeh

Reputation: 661

Try mod_pagespeed which may be of some use to you.

Upvotes: 29

Related Questions