首页 > 代码库 > Yii 2.x html 代码压缩

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;
        }
    }
}

 

Yii 2.x html 代码压缩