Store PHP script output in "compressed" HTML

Asked

Viewed 137 times

2

In a content manager, I usually generate static HTML pages each time they are visited, the script redoes the HTML when there is some change on this page, simply deleting the previously generated file.

When the user visits this page through the browser, is served the HTML version, that is, the script checks if there is any HTML already created, if yes then the same is included and the execution ends without performing any query or routine processing, if not, it does all the processing and saves an HTML to be served later (as a cache).

My question is whether I can do this using a compressed HTML using the function ob_start('ob_gzhandler') instead of just ob_start();

Is there a compatibility issue, or maybe you have a better method to do what I need?

1 answer

1


You can use an alternative form of compaction:

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")

Original question:

https://stackoverflow.com/questions/6225351/how-to-minify-php-page-html-output

Browser other questions tagged

You are not signed in. Login or sign up in order to post.