PHP Compressor
Date: January 15, 2011 01:11PM
What this does is it'll shrink down PHP code, in filesize. It does this by removing comments, unnecessary whitespaces and renaming all variables to be as short as possible.
My goal in writing it was more to complete it as fast as possible, not to make it as nice as possible. You have been warned.
If there are any problems, do report them though, I'll be happy to fix them. Improvements would also be nice. I've thought about changing custom function names too or perhaps making new functions for more commonly used ones in the script, but stuff like that you should be able to do yourself fairly easily too.
One last cautionary note: It semi-filters HTML too.
Really, all it does is this:
$tk2 = str_replace(array("\t",'<br />','<br/>'),array('','<br>','<br>'),$token_code);
while(strpos($tk2," ")) $tk2 = str_replace(' ',' ',$tk2);
That being said, here it is, give it a try (name it phpcompress.php):
<?php
function post($a) {
return isset($_POST[$a]) ? (get_magic_quotes_gpc() ? stripslashes($_POST[$a]) : $_POST[$a]) : null;
}
function varname($id) {
$n = base_convert($id, 10, 26);
$n = str_replace(array(0,1,2,3,4,5,6,7,8,9),array('q','r','s','t','u','v','w','x','y','z'),$n);
return $n;
}
function phpcompress($code) {
$vars_donotchange = array('_SESSION','_FILES','_POST','GLOBALS','_GET','_COOKIE','_FILES','_SERVER','_ENV','HTTP_SERVER_VARS','HTTP_ENV_VARS','HTTP_POST_VARS','HTTP_GET_VARS','_REQUEST');
$tokens = @token_get_all($code);
$newcode = "";
$mode = "";
$varnewnames = array();
$varid = 1;
$nowhitespace = array('KYO_CHAR','T_ECHO','T_ELSE','T_IS_SMALLER_OR_EQUAL','T_IS_NOT_EQUAL','T_LIST','T_NEW','T_RETURN','T_IS_GREATER_OR_EQUAL','T_STRING','T_LNUMBER','T_BOOLEAN_AND','T_FOREACH','T_IS_IDENTICAL','T_EXIT','T_WHILE','T_IS_NOT_IDENTICAL','T_BOOLEAN_OR','T_CASE','T_DOUBLE_ARROW','T_IF','T_VARIABLE','T_CONCAT_EQUAL','T_CONSTANT_ENCAPSED_STRING','T_AS','T_WHITESPACE','T_COMMENT','T_IS_EQUAL','T_OPEN_TAG');
$nowhitespace_checknext = array('T_ECHO','T_LIST','T_STRING','T_WHILE','T_NEW','T_RETURN','T_CASE','T_ELSE','T_IF','T_VARIABLE','T_AS','T_EXIT','T_FOREACH');
$nowhitespace_nextcool = array('KYO_CHAR','T_VARIABLE','T_CONSTANT_ENCAPSED_STRING','T_IS_NOT_EQUAL','T_CONCAT_EQUAL','T_BOOLEAN_AND','T_IS_SMALLER_OR_EQUAL','T_IS_GREATER_OR_EQUAL','T_IS_IDENTICAL','T_DOUBLE_ARROW','T_IS_EQUAL','T_IS_NOT_IDENTICAL');
foreach($tokens as $ti => $token) {
if(is_array($token)) {
$token_name = token_name($token[0]);
$token_code = $token[1];
}
else {
$token_name = "KYO_CHAR";
$token_code = $token;
}
if(isset($tokens[($ti+1)])) {
$next_token = $tokens[($ti+1)];
if(is_array($next_token)) {
$next_token_name = token_name($next_token[0]);
$next_token_code = $next_token[1];
}
else {
$next_token_name = "KYO_CHAR";
$next_token_code = $next_token;
}
} else $next_token = $next_token_name = false;
switch($token_name) {
default:
$newcode .= $token_code;
$mode = "";
break;
case "T_OPEN_TAG":
$newcode .= "<? ";
break;
case "T_COMMENT":
case "T_WHITESPACE":
if(!( in_array($prev_token_name,$nowhitespace) && (!in_array($prev_token_name,$nowhitespace_checknext) || (in_array($next_token_name,$nowhitespace_nextcool)) ) )) {
$newcode .= "\n";
}
break;
case "T_INLINE_HTML":
$tk2 = str_replace(array("\t",'<br />','<br/>'),array('','<br>','<br>'),$token_code);
while(strpos($tk2," ")) $tk2 = str_replace(' ',' ',$tk2);
$newcode .= $tk2;
break;
case "T_VARIABLE":
$varname = substr($token_code,1);
if($token_code == '$GLOBALS') {
$mode = "globalsvar_1";
$newcode .= $token_code;
} elseif(in_array($varname,$vars_donotchange)) $newcode .= $token_code;
else {
if(!isset($varnewnames[$varname])) {
$varnewnames[$varname] = varname($varid);
$varid++;
}
$newcode .= '$'.$varnewnames[$varname];
}
break;
case "KYO_CHAR":
if($mode == "globalsvar_1" && $token_code == "[") {
$mode = "globalsvar_2";
$newcode .= $token_code;
} else {
$newcode .= $token_code;
$mode = "";
}
break;
case "T_CONSTANT_ENCAPSED_STRING":
if($mode == "globalsvar_2") {
$varname = eval("return ".$token_code.";");
if(in_array($varname,$vars_donotchange)) {
$newcode .= $token_code;
}
elseif(!isset($varnewnames[$varname])) {
$varnewnames[$varname] = varname($varid);
$varid++;
$newcode .= var_export($varnewnames[$varname],true);
} else {
$newcode .= var_export($varnewnames[$varname],true);
}
$mode = "";
}
else $newcode .= $token_code;
break;
}
$prev_token_name = $token_name;
$prev_token_code = $token_code;
}
return $newcode;
}
if(post('code')) {
$code = post('code');
$newcode = phpcompress($code);
$charssaved = strlen($code) - strlen($newcode);
} else $newcode = "";
echo "<form method='post' action='phpcompress.php'><textarea name='code' style='width:100%;height:500px;'>".htmlentities($newcode)."</textarea><br /><input type='submit'></form>";
if(post('code')) {
echo "$charssaved characters saved. Total characters now: ".strlen($newcode)." (".strlen($code)." before)";
}
?>