首页 > Web开发 > 详细

Yii 2.x html 代码压缩

时间:2017-01-20 17:57:59      阅读:257      评论:0      收藏:0      [点我收藏+]

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!