压缩php代码,删除多余空白注释,无用空格,比tp处理彻底
/**
* @author 70(qq781787584)
*
* @link http://www.qiling.org
*
* @copyright 2014-12-09 00:32:10
*
*/
header ( content-type: text/html; charset=utf-8 );
// $content = file_get_contents ( 'demo.php' );
$content = file_get_contents ( __file__ );
$result = token_get_all ( $content );
// 遍历数组 使用指针
$string = '';
$space = false;
// print_r($result);
while ( current ( $result ) ) {
$value = current ( $result );
if (is_string ( $value )) {
// 去掉字符左侧的 空白
if ($space) {
$string = rtrim ( $string ) . $value;
} else {
$string .= $value;
}
// 去掉字符右侧的 空白 添加一个标记 如果为 true需要删除右侧的空白 如果 为 false 不需要删除右侧的空白
$space = true;
} else {
switch ($value [0]) {
// 去掉php开始标记中的空格
case t_open_tag :
$string .= trim ( $value [1] ) . ' ';
//这样做了能删除各种注释下的多余空格 如果没有下边这句会造成有多余空格 如 t_inc, // ++
t_dec, // --
t_plus_equal, // +=
t_minus_equal, // -=
t_mul_equal, // *=
t_div_equal, // /=
t_is_identical, // ===
t_is_not_identical, // !==
t_double_colon, // ::
t_paamayim_nekudotayim, // ::
t_object_operator, // ->
t_dollar_open_curly_braces, // ${
t_and_equal, // &=
t_mod_equal, // %=
t_xor_equal, // ^=
t_or_equal, // |=
t_sl, // t_sr, // >>
t_sl_equal, // t_sr_equal // >>=
);
if (in_array ( $value [0], $array )) {
$string = rtrim ( $string ) . $value [1];
;;;;;;;;;;; //有强迫症的可以继续删除 多余 的分号 我就不删除了
} else {
$string .= $value [1];
}
$space = in_array ( $value [0], $array );
break;
}
}
next ( $result );
}
echo $string;转载注明我的博客地址:http://www.qiling.org
ad:真正免费,域名+虚机+企业邮箱=0元