首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何缩小php页面html输出?

如何缩小php页面html输出?
EN

Stack Overflow用户
提问于 2011-06-03 17:36:07
回答 9查看 182.2K关注 0票数 154

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

我该怎么做呢?

EN

回答 9

Stack Overflow用户

发布于 2011-06-03 17:39:23

如果要正确执行此操作,请启用gzip。你也可以这样做:

代码语言:javascript
复制
$this->output = preg_replace(
    array(
        '/ {2,}/',
        '/<!--.*?-->|\t|(?:\r?\n[ \t]*)+/s'
    ),
    array(
        ' ',
        ''
    ),
    $this->output
);

通过将html转换为一行,没有制表符,没有新行,没有注释,这就减少了大约30%的页面大小。里程可能会有所不同

票数 26
EN

Stack Overflow用户

发布于 2015-03-31 16:02:51

我已经试过几个迷你,它们要么移除太少,要么移除太多。

这段代码删除了多余的空格和可选的HTML (结束)标记。它也很安全,不会删除任何可能破坏HTML、JS或CSS的东西。

此外,代码还展示了如何在Zend Framework中做到这一点:

代码语言:javascript
复制
class Application_Plugin_Minify extends Zend_Controller_Plugin_Abstract {

  public function dispatchLoopShutdown() {
    $response = $this->getResponse();
    $body = $response->getBody(); //actually returns both HEAD and BODY

    //remove redundant (white-space) characters
    $replace = array(
        //remove tabs before and after HTML tags
        '/\>[^\S ]+/s'   => '>',
        '/[^\S ]+\</s'   => '<',
        //shorten multiple whitespace sequences; keep new-line characters because they matter in JS!!!
        '/([\t ])+/s'  => ' ',
        //remove leading and trailing spaces
        '/^([\t ])+/m' => '',
        '/([\t ])+$/m' => '',
        // remove JS line comments (simple only); do NOT remove lines containing URL (e.g. 'src="http://server.com/"')!!!
        '~//[a-zA-Z0-9 ]+$~m' => '',
        //remove empty lines (sequence of line-end and white-space characters)
        '/[\r\n]+([\t ]?[\r\n]+)+/s'  => "\n",
        //remove empty lines (between HTML tags); cannot remove just any line-end characters because in inline JS they can matter!
        '/\>[\r\n\t ]+\</s'    => '><',
        //remove "empty" lines containing only JS's block end character; join with next line (e.g. "}\n}\n</script>" --> "}}</script>"
        '/}[\r\n\t ]+/s'  => '}',
        '/}[\r\n\t ]+,[\r\n\t ]+/s'  => '},',
        //remove new-line after JS's function or condition start; join with next line
        '/\)[\r\n\t ]?{[\r\n\t ]+/s'  => '){',
        '/,[\r\n\t ]?{[\r\n\t ]+/s'  => ',{',
        //remove new-line after JS's line end (only most obvious and safe cases)
        '/\),[\r\n\t ]+/s'  => '),',
        //remove quotes from HTML attributes that does not contain spaces; keep quotes around URLs!
        '~([\r\n\t ])?([a-zA-Z0-9]+)="([a-zA-Z0-9_/\\-]+)"([\r\n\t ])?~s' => '$1$2=$3$4', //$1 and $4 insert first white-space character found before/after attribute
    );
    $body = preg_replace(array_keys($replace), array_values($replace), $body);

    //remove optional ending tags (see http://www.w3.org/TR/html5/syntax.html#syntax-tag-omission )
    $remove = array(
        '</option>', '</li>', '</dt>', '</dd>', '</tr>', '</th>', '</td>'
    );
    $body = str_ireplace($remove, '', $body);

    $response->setBody($body);
  }
}

但请注意,当使用gZip压缩时,您的代码比任何缩小都要压缩得多,组合缩小和gZip是没有意义的,因为下载节省的时间会因为缩小而损失,也会节省最少的时间。

以下是我的结果(通过3G网络下载):

代码语言:javascript
复制
 Original HTML:        150kB       180ms download
 gZipped HTML:          24kB        40ms
 minified HTML:        120kB       150ms download + 150ms minification
 min+gzip HTML:         22kB        30ms download + 150ms minification
票数 23
EN

Stack Overflow用户

发布于 2014-05-09 09:29:47

上面所有的preg_replace()解决方案都有单行注释、条件注释和其他陷阱的问题。我建议您利用经过良好测试的Minify project,而不是从头开始创建您自己的正则表达式。

在我的例子中,我将下面的代码放在PHP页面的顶部来缩小它:

代码语言:javascript
复制
function sanitize_output($buffer) {
    require_once('min/lib/Minify/HTML.php');
    require_once('min/lib/Minify/CSS.php');
    require_once('min/lib/JSMin.php');
    $buffer = Minify_HTML::minify($buffer, array(
        'cssMinifier' => array('Minify_CSS', 'minify'),
        'jsMinifier' => array('JSMin', 'minify')
    ));
    return $buffer;
}
ob_start('sanitize_output');
票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6225351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档