如何缩小php页面的html输出? [英] How to minify php page html output?

查看:110
本文介绍了如何缩小php页面的html输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个php脚本或类,可以缩小我的php页面html输出,如google页面速​​度。



我该怎么做?

解决方案

CSS和Javascript



请考虑以下链接以缩小Javascript / CSS文件: https://github.com/mrclay/minify



HTML



告诉Apache使用GZip传递HTML - 这通常会将响应大小减少约70%。 (如果使用Apache,配置gzip的模块取决于您的版本:Apache 1.3使用mod_gzip,而Apache 2.x使用mod_deflate。)


Accept-Encoding:gzip,deflate

内容编码:gzip

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

 <?php 

函数sanitize_output($ buffer){
$ b $ search = array(
'/ \> [^ \ s] + / s',//标签之后的空格,空格除外
'/ [^ \\ \\ s] + \< / s',//在标签之前剥离空格,空格除外
'/(\ s)+ / s',//缩短多个空格序列
'/< ;! - (。| \s)*? - > /'//移除HTML注释
);

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

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

返回$ buffer;
}

ob_start(sanitize_output);

?>


I am looking for a php script or class that can minify my php page html output like google page speed does.

How can I do this?

解决方案

CSS and Javascript

Consider the following link to minify Javascript/CSS files: https://github.com/mrclay/minify

HTML

Tell Apache to deliver HTML with GZip - this generally reduces the response size by about 70%. (If you use Apache, the module configuring gzip depends on your version: Apache 1.3 uses mod_gzip while Apache 2.x uses mod_deflate.)

Accept-Encoding: gzip, deflate

Content-Encoding: gzip

Use the following snippet to remove white-spaces from the HTML with the help ob_start's buffer:

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

?>

这篇关于如何缩小php页面的html输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆