PHPページのHTML出力を縮小するにはどうすればいいですか? 質問する

PHPページのHTML出力を縮小するにはどうすればいいですか? 質問する

Google Page Speed のように PHP ページの HTML 出力を縮小できる PHP スクリプトまたはクラスを探しています。

これどうやってするの?

ベストアンサー1

CSSとJavascript

Javascript/CSS ファイルを縮小するには、次のリンクを検討してください。https://github.com/mrclay/minify

html

Apache に HTML を GZip で配信するように指示します。これにより、通常、応答サイズが約 70% 削減されます。(Apache を使用する場合、gzip を構成するモジュールはバージョンによって異なります。Apache 1.3 では mod_gzip が使用され、Apache 2.x では mod_deflate が使用されます。)

Accept-Encoding: gzip、deflate

コンテンツエンコーディング: gzip

使用次のスニペットob_start のバッファを利用して HTML から空白を削除します。

<?php

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
        '/<!--(.|\s)*?-->/' // Remove HTML comments
    );

    $replace = array(
        '>',
        '<',
        '\\1',
        ''
    );

    $buffer = preg_replace($search, $replace, $buffer);

    return $buffer;
}

ob_start("sanitize_output");

?>

おすすめ記事