thinkphp5框架压缩前端输出的代码

2021-05-26
193次阅读
没有评论

下面说的有点乱,方法就是函数,函数就是方法,在php里面通常都是叫函数的,我个人喜欢叫方法。

查了一下资料,thinkphp5实现压缩代码大部分都是需要导入某个类只有再输出,我这里给一个方法用php函数处理前端代码。

首先,需要了解的一个原理,thinkphp输出代码的原理是controller里面的方法渲染到tp的view模块进行输出的,所以源码还是需要通过php进行处理的。既然知道什么原理了,那就弄一个方法来处理我们需要输出的内容。

我的做法是新建一个function 方法,把这个方法放到公共类里面,最好是全局调用的那种,因为各大方法都需要调用。

我放在自己定义的 application/common.php 公共文件中了。之后直接在方法里面调用即可。

1.修改方法

首先,thinkphp5的方法输出 return view(); 修改为 $this->fetch();  因为 view 输出报错,换一个方式就没问题了。

2.方法调用处理代码的函数

return view();

修改为

return yasuo_html($this->fetch());

这个yasuo_html() 就是我们这次的主要使用的函数,也是处理html压缩的函数方法。方法代码会放在最后。

处理完的效果就是下面图片的这种。

thinkphp5框架压缩前端输出的代码

3.处理类的函数代码

// 压缩前端代码  https://www.linpx.com/p/pinghsu-subject-integration-code-compression.html
function yasuo_html($html_source) {
    $chunks = preg_split('/(<!--<nocompress>-->.*?<!--</nocompress>-->|<nocompress>.*?</nocompress>|<pre.*?/pre>|<textarea.*?/textarea>|<script.*?/script>)/msi', $html_source, -1, PREG_SPLIT_DELIM_CAPTURE);
    $compress = '';
    foreach ($chunks as $c) {
        if (strtolower(substr($c, 0, 19)) == '<!--<nocompress>-->') {
            $c = substr($c, 19, strlen($c) - 19 - 20);
            $compress .= $c;
            continue;
        } else if (strtolower(substr($c, 0, 12)) == '<nocompress>') {
            $c = substr($c, 12, strlen($c) - 12 - 13);
            $compress .= $c;
            continue;
        } else if (strtolower(substr($c, 0, 4)) == '<pre' || strtolower(substr($c, 0, 9)) == '<textarea') {
            $compress .= $c;
            continue;
        } else if (strtolower(substr($c, 0, 7)) == '<script' && strpos($c, '//') != false && (strpos($c, "r") !== false || strpos($c, "n") !== false)) {
            $tmps = preg_split('/(r|n)/ms', $c, -1, PREG_SPLIT_NO_EMPTY);
            $c = '';
            foreach ($tmps as $tmp) {
                if (strpos($tmp, '//') !== false) {
                    if (substr(trim($tmp), 0, 2) == '//') {
                        continue;
                    }
                    $chars = preg_split('//', $tmp, -1, PREG_SPLIT_NO_EMPTY);
                    $is_quot = $is_apos = false;
                    foreach ($chars as $key => $char) {
                        if ($char == '"' && $chars[$key - 1] != '' && !$is_apos) {
                            $is_quot = !$is_quot;
                        } else if ($char == ''' && $chars[$key - 1] != '' && !$is_quot) {
                            $is_apos = !$is_apos;
                        } else if ($char == '/' && $chars[$key + 1] == '/' && !$is_quot && !$is_apos) {
                            $tmp = substr($tmp, 0, $key);
                            break;
                        }
                    }
                }
                $c .= $tmp;
            }
        }
        $c = preg_replace('/[nrt]+/', ' ', $c);
        $c = preg_replace('/s{2,}/', ' ', $c);
        $c = preg_replace('/>s</', '> <', $c);
        // 去除注释(可选)
        // $c = preg_replace('//*.*?*//i', '', $c);
        // $c = preg_replace('/<!--[^!]*-->/', '', $c);
        $compress .= $c;
    }
    return $compress;
}

此代码是从本博客的主题中提取出来的,作者为孟坤,处理效果不错,当然网上随便找的代码也有这种效果。看个人喜好。

正文结束
居烽
版权声明:本站原创文章,由 居烽 于2021-05-26发表,共计2122字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码