Yii 2.x html 代码压缩

 

<?php

namespace Pangu\web;

use yii\base\Component;

/**
 * html格式响应内容格式化
 * @author zhouzhian
 *
 */
class HtmlResponseFormatter extends \yii\web\HtmlResponseFormatter
{

    /**
     * 格式化响应内容
     */
    public function format($response)
    {
        if (stripos($this->contentType, 'charset') === false) {
            $this->contentType .= '; charset=' . $response->charset;
        }
        $response->getHeaders()->set('Content-Type', $this->contentType);
        if ($response->data !== null) {
            $tmplContent = $response->data;
            // 去除html空格与换行
            $find           = array("~>\s+<~","~>(\s+\n|\r)~");
            $replace        = array('><','>');
            $tmplContent    = preg_replace($find, $replace, $tmplContent);
            $response->content = $tmplContent;
        }
    }
}

 

posted on 2017-01-20 16:32  周~~  阅读(408)  评论(0编辑  收藏  举报

导航