Gen. partnerAlgotech

Automatic minification of PHP script

Sometimes we need to shrink a large PHP script and compress several of them into one file. This comes in handy when we are creating a library that we will publish to the web and don't want anyone to interfere with it, or it is a useful script that we will be copying often and don't want to transfer too much data.

|
August 22, 2019

Sometimes we need to shrink a large PHP script and compress several of them into one file. This comes in handy when we are creating a library that we will publish to the web and don't want anyone to interfere with it, or it is a useful script that we will be copying often and don't want to transfer too much data.

A possible solution is to minify the code.

I have prepared an online tool for this (just paste the code and you will get back a minified version of it immediately).

The core of the minifier can be reduced to this minimum:

$file = 'StaticClass.php';
// Dgx's PHP shrinker
// PHP 4 & 5 compatibility
if (!defined('T_DOC_COMMENT'))
define ('T_DOC_COMMENT', -1);
if (!defined('T_ML_COMMENT'))
define ('T_ML_COMMENT', -1);
// read input file
$input = file_get_contents($file);
$space = $output = '';
$set = '!"#$&()*+,-./:;<=>?@[\]^`{|}';
$set = array_flip(preg_split('//',$set));
foreach (token_get_all($input) as $token) {
if (!is_array($token))
$token = array(0, $token);
switch ($token[0]) {
case T_COMMENT:
case T_ML_COMMENT:
case T_DOC_COMMENT:
case T_WHITESPACE:
$space = ' ';
break;
default:
if (isset($set[substr($output, -1)]) ||
isset($set[$token[1]{0}])) $space = '';
$output .= $space . $token[1];
$space = '';
}
}
// write shrinked file
file_put_contents('min_'.$file, $output);

The core is the token_get_all() function, which parses PHP code into individual "atoms" (tokens) that can be uniquely identified and then ignored as needed.

For example (I used the Nette\Utils\Images method for the example):

Minified code
Loading comments...

Stay in the loop

Subscribe to our newsletter and get the latest cybersecurity news delivered straight to your inbox.

Your data is safe. You can unsubscribe from the newsletter at any time.