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

来源:互联网 发布:卫浴淘宝官方 编辑:程序博客网 时间:2024/05/29 02:25
<?phpheader("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;}


0 0
原创粉丝点击