复制代码 代码如下:
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]){
//过滤各种php注释
case t_comment:
case t_doc_comment:
break;
//过滤空格
case t_whitespace:
if (!$last_space){
$stripstr .= ' ';
$last_space = true;
}
break;
default:
$last_space = false;
$stripstr .= $tokens[$i][1];
}
}
}
return $stripstr;
}
该自定义函数有效解决了php_strip_whitespace系统内置去注释空格函数不能正确理解<<<eot(heredoc)的问题
使用方法
复制代码 代码如下:
$str = strip_whitespace('<?php'.$str);
前面一定要拼接这个,我也搞不懂,不拼接的话执行生成的结果是错误的结果
php_strip_whitespace
string php_strip_whitespace (string$filename )
如果仅仅是单文件并且没有heredoc的话,还是建议使用快捷的php_strip_whitespace函数
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!