1

I have a site with the files all being php files, on the server we also have htaccess doing rewrites for friendly urls, i.e. /about

We already have minification by PHP for JS and CSS, however due to the fact the HTML is on the actual page is there a way of minifying the html after the php has added in the dynamic bits of the page but before the server sends it back to the client?

I have tried implementing minify, however the callback function breaks the page with no errors.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
André Figueira
  • 6,048
  • 14
  • 48
  • 62

4 Answers4

7

Use this before <doctype html> tag or config file

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;
}
ob_start("sanitize_output");
Krishna Torque
  • 623
  • 1
  • 7
  • 17
  • This should not be applied "before ``" (does not make any sense if output has started), but at the very start of your PHP script. – BenMorel Jul 16 '18 at 11:06
5

If we're talking about dynamically minifying everything on the fly, I would council you to be cautious about seeing this as an optimisation.

Yes, there may be a significant bandwidth saving to be had. And yes, it is possible that your users may get a performance boost from the smaller downloads.

But, if you're minifying your files on the fly using PHP (which is what it sounds like you're doing), it is also possible that you may actually be slowing things down for the end user.

Minification is best done on static files. Minify them once, save them, and serve the minified file to the browser without any further processing when the file is requested. Doing it in PHP every time a file requested can be a significant performance overhead which could easily counteract the gains from the smaller download size.

Minifying HTML is usually less effective than Javascript. The best way to 'minify' your PHP-generated HTML is to cut out the unnecessary white space from your HTML template files. This is usually a pretty easy task, and doesn't need a dedicated minifier tool -- simplest way to do it is just to remove all indentation from the file. White space is relevant in HTML, so you can't just get rid of it all, but losing the indentation should deal with most of it, while conveniently still leaving the file reasonable readable.

So my advice is to minify your files statically if possible. But if you must do it on the fly, don't use PHP for the job. Use a dedicated server plug in, such as Google's mod_pagespeed. This plugin will minify the files for you when they're requested (at much higher speed than PHP could do it), and also takes care of caching the minified version, to minimise the performance impact.

Hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307
0

You could look into output buffering in PHP, which would allow you the final minification step. If your aims is to minimise for download there's also gZip encoding to look into.

This may help: how to minify php page html output

Community
  • 1
  • 1
A.M. D.
  • 928
  • 6
  • 10
  • Thanks I already tried this but I was having an issue where every time i tried running the output buffer callback it would break my page and not return any errors. – André Figueira Nov 12 '12 at 20:30
  • What error level was this run at, do you get any errors when you add more stringent error notifiers? Anything logged? – A.M. D. Nov 12 '12 at 20:42
  • I found out what the issue was, well more a less, the problem is being caused by the minification package, replacing the callback with something else works perfectly, so going to run a unit test on that, will accept your answer anyway as was the most helpful! :) – André Figueira Nov 12 '12 at 20:47
0

There is one script I came across recently that used to minify your HTML source code on the fly, script called as Dynamic Website Compressor

it's a PHP based script, but can be used with .htaccess

Basic Bridge
  • 1,881
  • 4
  • 25
  • 37