tokenizer内置于PHP本身怎么样?
The tokenizer functions provide an
interface to the PHP tokenizer
embedded in the Zend Engine. Using
these functions you may write your own
PHP source analyzing or modification
tools without having to deal with the
language specification at the lexical
level.
您在评论中询问是否可以从标记化输出中重新生成代码 – 但是您可以将所有空格保留为T_WHITESPACE标记.以下是将标记化输出转换回代码的方法:
$regenerated='';
$tokens = token_get_all($code);
foreach($tokens as $idx=>$t)
{
if (is_array($t))
{
//do something with string and comments here?
switch($t[0])
{
case T_CONSTANT_ENCAPSED_STRING:
break;
case T_COMMENT:
case T_DOC_COMMENT:
break;
}
$regenerated.=$t[1];
}
else
{
$regenerated.=$t;
}
}