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

压缩php代码,删除多余空白注释,无用空格,比TP处理彻底

WBOY
Release: 2016-06-07 11:40:19
Original
1305 people have browsed it

压缩php代码,删除多余空白注释,无用空格,比TP处理彻底
<?php <br /> /** <br>  * @Author 70(QQ781787584)<br>  *  <br>  * @link http://www.qiling.org <br>  * <br>  * @Copyright 2014-12-09 00:32:10 <br>  * <br>  */<br> header ( "Content-type: text/html; charset=utf-8" );<br> // $content = file_get_contents ( 'demo.php' );<br> $content = file_get_contents ( __FILE__ );<br> $result = token_get_all ( $content );<br> <br> // 遍历数组 使用指针<br> $string = '';<br> $space = false;<br> // print_r($result);<br> <br> while ( current ( $result ) ) {<br>     $value = current ( $result );<br>     if (is_string ( $value )) {<br>         // 去掉字符左侧的 空白<br>         if ($space) {<br>             $string = rtrim ( $string ) . $value;<br>         } else {<br>             $string .= $value;<br>         }<br>         // 去掉字符右侧的 空白 添加一个标记  如果为 True需要删除右侧的空白 如果 为 false 不需要删除右侧的空白<br>         $space = true;<br>     } else {<br>         switch ($value [0]) {<br>             // 去掉php开始标记中的空格<br>             case T_OPEN_TAG :<br>                 $string .= trim ( $value [1] ) . ' ';<br>                 //这样做了能删除各种注释下的多余空格 如果没有下边这句会造成有多余空格  如 <?php /**/,<?php // , <?php echo <br />                 //虽然我这样做了 ^_^ 但是我没想明白...<br>                 $space = true;<br>                 break;<br>             // 把空白字符全部转换为 空格<br>             case T_WHITESPACE :<br>                 if ($space == false) {<br>                     $string .= ' ';<br>                     $space = true;<br>                 }<br>                 break;<br>             // 去掉注释<br>             case T_DOC_COMMENT :<br>                 $space = true;<br>                 break;<br>             // 去掉注释<br>             case T_COMMENT :<br>                 $space = true;<br>                 break;<br>             // 判断定界符开始<br>             case T_START_HEREDOC :<br>                 $space = false;<br>                 $string .= "                 break;<br>             // 判断定界符结束<br>             case T_END_HEREDOC :<br>                 $space = true;<br>                 $string .= "S;\n";<br>                 //因为这里取到的值是不带 分号 ; 的 这里直接 跳过下个元素的处理 注:不知道会不会存在问题  测试没问题<br>                 next ( $result );<br>                 <br>                 break;<br>             default :<br>                 // 去掉某些 左右 的空白 你可以添加更多你认为两边可以删除空白的标记 目前我找到这些<br>                 $array = array (<br>                         T_CONCAT_EQUAL, // .=<br>                         T_DOUBLE_ARROW, // =><br>                         T_BOOLEAN_AND, // &&<br>                         T_BOOLEAN_OR, // ||<br>                         T_IS_EQUAL, // ==<br>                         T_IS_NOT_EQUAL, // != or <br>                         T_IS_SMALLER_OR_EQUAL, //                          T_IS_GREATER_OR_EQUAL, // >=<br>                         T_INC, // ++<br>                         T_DEC, // --<br>                         T_PLUS_EQUAL, // +=<br>                         T_MINUS_EQUAL, // -=<br>                         T_MUL_EQUAL, // *=<br>                         T_DIV_EQUAL, // /=<br>                         T_IS_IDENTICAL, // ===<br>                         T_IS_NOT_IDENTICAL, // !==<br>                         T_DOUBLE_COLON, // ::<br>                         T_PAAMAYIM_NEKUDOTAYIM, // ::<br>                         T_OBJECT_OPERATOR, // -><br>                         T_DOLLAR_OPEN_CURLY_BRACES, // ${<br>                         T_AND_EQUAL, // &=<br>                         T_MOD_EQUAL, // %=<br>                         T_XOR_EQUAL, // ^=<br>                         T_OR_EQUAL, // |=<br>                         T_SL, //                          T_SR, // >><br>                         T_SL_EQUAL, //                          T_SR_EQUAL  // >>=<br>                 );<br>                 <br>                 if (in_array ( $value [0], $array )) {<br>                     $string = rtrim ( $string ) . $value [1];<br>                     ;;;;;;;;;;; //有强迫症的可以继续删除 多余 的分号 我就不删除了<br>                 } else {<br>                     $string .= $value [1];<br>                 }<br>                 $space = in_array ( $value [0], $array );<br>                 break;<br>         }<br>     }<br>     next ( $result );<br> }<br> <br> echo $string;转载注明我的博客地址:http://www.qiling.org

AD:真正免费,域名+虚机+企业邮箱=0元

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