php如何最小化页面html输出

2,569次阅读

共计 462 个字符,预计需要花费 2 分钟才能阅读完成。

CSS and Javascript

minify 类库来缩小 Javascript / CSS 文件

HTML

我们可以启用 gzip 来压缩

使用以下代码片段,使用帮助 ob_start 的缓冲区从 HTML 中删除空格:

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

正文完
 
Blood.Cold
版权声明:本站原创文章,由 Blood.Cold 2019-06-02发表,共计462字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。