When my scripts are done, I want to optimize/convert them to smaller size + its harder to get know what do the files even they are stolen.
$c = file_get_contents('source.php');
$newStr = '';
$commentTokens = array(T_COMMENT);
if (defined('T_DOC_COMMENT'))
$commentTokens[] = T_DOC_COMMENT; // PHP 5
if (defined('T_ML_COMMENT'))
$commentTokens[] = T_ML_COMMENT; // PHP 4
$tokens = token_get_all($c);
foreach ($tokens as $token) {
if (is_array($token)) {
if (in_array($token[0], $commentTokens))
continue;
$token = $token[1];
}
$newStr .= $token;
}
$newStr = str_replace (chr(13), '', $newStr);
$newStr = str_replace (chr(10), '', $newStr);
$newStr = preg_replace('/\s+/', ' ', $newStr);
now $newStr
contain the "compressed" stuff. Almost OK, but it kills to much white spaces. If there are white spaces in code like this:
if (true)
{
codeeee();
}
it converts to:
if (true)
{
codeeee();
}
and thats ok. But in case of this:
$a = ' var ';
it does:
$a = ' var ';
which is unwanted. How to do this optimize correctly? Are there any ideas? I almost thinking of renaming class names etc.