PHP file removes PHP annotation whitespace function analysis of PHP code compression
- 2020-06-23 00:06:40
- OfStack
Recently, I learned from thinkphp's 'RUNTIME_ALLINONE' experience: not afraid of cache, afraid of call chaos, simply all commonly used files are merged into one file, isn't it beautiful...
This custom function effectively solves the problem that the php_strip_whitespace system built-in annotation space function cannot be understood correctly < < < Question of EOT (heredoc)
Method of use
The first 1 has to concatenate this, and I don't get it, because if you don't concatenate it, you're going to get the wrong result
php_strip_whitespace
string php_strip_whitespace (string$filename )
If it is just a single file and there is no heredoc, the fast php_strip_whitespace function is recommended
function strip_whitespace($content) {
$stripStr = '';
// Analysis of the php The source code
$tokens = token_get_all ($content);
$last_space = false;
for ($i = 0, $j = count ($tokens); $i < $j; $i++){
if (is_string ($tokens[$i])){
$last_space = false;
$stripStr .= $tokens[$i];
}
else{
switch ($tokens[$i][0]){
// Filter a variety of PHP annotation
case T_COMMENT:
case T_DOC_COMMENT:
break;
// Filtering the blank space
case T_WHITESPACE:
if (!$last_space){
$stripStr .= ' ';
$last_space = true;
}
break;
default:
$last_space = false;
$stripStr .= $tokens[$i][1];
}
}
}
return $stripStr;
}
This custom function effectively solves the problem that the php_strip_whitespace system built-in annotation space function cannot be understood correctly < < < Question of EOT (heredoc)
Method of use
$str = strip_whitespace('<?php'.$str);
The first 1 has to concatenate this, and I don't get it, because if you don't concatenate it, you're going to get the wrong result
php_strip_whitespace
string php_strip_whitespace (string$filename )
If it is just a single file and there is no heredoc, the fast php_strip_whitespace function is recommended