您的位置:首页 > 编程语言 > PHP开发

php 去除注释压缩 heredoc去注释不压缩

2015-04-08 16:10 155 查看
<?php
header("content-type:text/html;charset=utf-8");

$str = "./hom/ChapterAction.class.php";
file_put_contents("./abc.php",replace_php_src($str));

function replace_php_src($src){
    $contents = file_get_contents($src);
    $num = substr_count($contents,'<<<');   // heredoc 是否存在。
    $str = "";
    if($num > 0){    // heredoc 存在。只去除注释不压缩。
            $file = token_get_all($contents);  // token_get_all() 将提供的源码按 PHP 标记进行分割.
            for ($i=0; $i < count($file); $i++) {
                if( is_string($file[$i]) ){
                    $str .= $file[$i];
                }else{
                      $name = token_name( $file[$i][0] ); // token_name() 获取提供的 PHP 解析器代号的符号名称.
                       if($name == 'T_COMMENT' || $name == 'T_DOC_COMMENT' ){  // 去除注释
                                continue;
                       }else{
                            $str .= $file[$i][1];
                       }
                }
            } 
    }else{
        $str =  php_strip_whitespace($src);  // 不存在 heredoc 。因为他会错误解析。
    }
    return $str;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: