|
復(fù)制代碼 代碼如下:
function strip_whitespace($content) {
$stripStr = '';
//分析php源碼
$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]){
//過(guò)濾各種php注釋
case T_COMMENT:
case T_DOC_COMMENT:
break;
//過(guò)濾空格
case T_WHITESPACE:
if (!$last_space){
$stripStr .= ' ';
$last_space = true;
}
break;
default:
$last_space = false;
$stripStr .= $tokens[$i][1];
}
}
}
return $stripStr;
}
該自定義函數(shù)有效解決了php_strip_whitespace系統(tǒng)內(nèi)置去注釋空格函數(shù)不能正確理解<<<EOT(heredoc)的問(wèn)題
使用方法
復(fù)制代碼 代碼如下:
$str = strip_whitespace('<?php'.$str);
前面一定要拼接這個(gè),我也搞不懂,不拼接的話執(zhí)行生成的結(jié)果是錯(cuò)誤的結(jié)果
php_strip_whitespace
string php_strip_whitespace (string$filename )
如果僅僅是單文件并且沒(méi)有heredoc的話,還是建議使用快捷的php_strip_whitespace函數(shù)
php技術(shù):PHP文件去掉PHP注釋空格的函數(shù)分析(PHP代碼壓縮),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。