Home > Backend Development > PHP Tutorial > Function analysis to remove PHP comment spaces from PHP files (PHP code compression)_PHP tutorial

Function analysis to remove PHP comment spaces from PHP files (PHP code compression)_PHP tutorial

WBOY
Release: 2016-07-21 15:01:02
Original
816 people have browsed it

Recently, I learned from thinkphp's 'RUNTIME_ALLINONE' experience: I am not afraid of too many caches, but I am afraid of chaotic calls. Wouldn't it be nice to simply merge all commonly used files into one file? . .

Copy code The code is as follows:

function strip_whitespace($content) {
$stripStr = '';
//Analyze php 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 various PHP comments
case T_COMMENT:
case T_DOC_ COMMENT:
break;
/ /Filter spaces
case T_WHITESPACE:
if (!$last_space){
$stripStr .= ' ';
                                                                                     :
                      $last_space = false;                                                         🎜> return $stripStr;
}


This custom function effectively solves the problem that the built-in comment space function of the php_strip_whitespace system cannot correctly understand <<
How to use it



Copy code

The code is as follows:
$str = strip_whitespace(' You must splice this before. I don’t understand it. If you don’t splice it, the generated result will be wrong.php_strip_whitespace
string php_strip_whitespace (string$filename)

If it is just a single file and there is no heredoc, it is recommended to use the quick
php_strip_whitespace
function



http://www.bkjia.com/PHPjc/328031.html
www.bkjia.com
true

http: //www.bkjia.com/PHPjc/328031.html

Recently I learned from thinkphp's 'RUNTIME_ALLINONE' experience: I am not afraid of too many caches, but I am afraid of chaotic calls, so I simply put all Wouldn't it be nice to combine all commonly used files into one file? . . Copy...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template