两个压缩在PHP中

时间:2018-06-18 03:29:49

标签: php ob-start

我目前正在浏览器中压缩代码和文件。我能做两次压缩吗?比如压缩代码并添加ob_gzhandler以便它Content-Encoding: gzip中的ob_start

这是我的代码:

<?php
trait traitViews {
    public function view($pageview, $noParent = false) {

        function code_compress($code) {
            $search = array('/>[^\S ]+/', // remove whitespaces after tags
                            '/[^\S ]+</', // remove whitespaces before tags
                            '/(\s)+/'); // remove multiple whitespace sequences
            $replace = array('>', '<',  '\\1');

            return preg_replace($search, $replace, $code);
        }

        ob_start('code_compress');  // I PLAN TO DO THIS 
                                   // ob_start('code_compress', 'ob_gzhandler'); 
                                  // but it gives me an error.
            header('Cache-Control: no-cache');
            header('Pragma: no-cache');
            if($noParent == false) {
                require_once('extras/html/entities/public_view/header.php');
                require_once('views/' . $pageview . '.php');
                require_once('extras/html/entities/public_view/footer.php');
            } else {
                require_once('views/' . $pageview . '.php');
            }
        ob_end_flush();
    }
}

class Views {
    use traitViews;
}

1 个答案:

答案 0 :(得分:0)

在这种情况下,它可以使用两个ob_start()

<?php
 ob_start('ob_gzhandler');
  ob_start('code_compression');
    /* YOUR OTHER CODE HERE */
  ob_end_flush();
 ob_end_flush();
?>
相关问题