Home > php教程 > php手册 > body text

PHP文件去掉PHP注释空格的函数分析(PHP代码压缩)

WBOY
Release: 2016-06-13 11:43:08
Original
822 people have browsed it

最近我从thinkphp的‘RUNTIME_ALLINONE'借鉴到经验:不怕缓存多,就怕调用乱,索性将所有常用的文件全部合并成一个文件,岂不美哉。。。

复制代码 代码如下:


function strip_whitespace($content) {
    $stripStr = '';
    //分析php源码
    $tokens =   token_get_all ($content);
    $last_space = false;
    for ($i = 0, $j = count ($tokens); $i         if (is_string ($tokens[$i])){
            $last_space = false;
            $stripStr .= $tokens[$i];
        }
        else{
            switch ($tokens[$i][0]){
                //过滤各种PHP注释
                case T_COMMENT:
                case T_DOC_COMMENT:
                    break;
                //过滤空格
                case T_WHITESPACE:
                    if (!$last_space){
                        $stripStr .= ' ';
                        $last_space = true;
                    }
                    break;
                default:
                    $last_space = false;
                    $stripStr .= $tokens[$i][1];
            }
        }
    }
    return $stripStr;
}


该自定义函数有效解决了php_strip_whitespace系统内置去注释空格函数不能正确理解使用方法

复制代码 代码如下:


$str = strip_whitespace('


前面一定要拼接这个,我也搞不懂,不拼接的话执行生成的结果是错误的结果
php_strip_whitespace
string php_strip_whitespace (string$filename )
如果仅仅是单文件并且没有heredoc的话,还是建议使用快捷的php_strip_whitespace函数
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!