码迷,mamicode.com
首页 > Web开发 > 详细

Yii 2.x html 代码压缩

时间:2017-01-20 17:58:33      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:div   base   ext   namespace   eth   bsp   find   charset   pangu   

 

<?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 代码压缩

标签:div   base   ext   namespace   eth   bsp   find   charset   pangu   

原文地址:http://www.cnblogs.com/xiaoyaogege/p/6323216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!