函数token_get_all不显示任何标记

时间:2014-06-17 11:44:44

标签: php

我有这个简单的代码来测试token_get_all的输出......

$arr = token_get_all("<?php $array=array(1,2,3); foreach($array as $key => $value) print($value); ?>");

print("<pre>");
print_r($arr);
print("</pre>");

但最终显示的是:

Array
(
[0] => Array
    (
        [0] => 372
        [1] =>  1
    )

[1] => =
[2] => Array
    (
        [0] => 362
        [1] => array
        [2] => 1
    )

[3] => (
[4] => Array
    (
        [0] => 305
        [1] => 1
        [2] => 1
    )

[5] => ,
[6] => Array
    (
        [0] => 305
        [1] => 2
        [2] => 1
    )

[7] => ,
[8] => Array
    (
        [0] => 305
        [1] => 3
        [2] => 1
    )

[9] => )
[10] => ;
[11] => Array
    (
        [0] => 375
        [1] =>  
        [2] => 1
    )

[12] => Array
    (
        [0] => 322
        [1] => foreach
        [2] => 1
    )

[13] => (
[14] => Array
    (
        [0] => 375
        [1] =>  
        [2] => 1
    )

[15] => Array
    (
        [0] => 326
        [1] => as
        [2] => 1
    )

[16] => Array
    (
        [0] => 375
        [1] =>   
        [2] => 1
    )

[17] => Array
    (
        [0] => 360
        [1] => =>
        [2] => 1
    )

[18] => Array
    (
        [0] => 375
        [1] =>  
        [2] => 1
    )

[19] => )
[20] => Array
    (
        [0] => 375
        [1] =>  
        [2] => 1
    )

[21] => Array
    (
        [0] => 266
        [1] => print
        [2] => 1
    )

[22] => (
[23] => )
[24] => ;
[25] => Array
    (
        [0] => 375
        [1] =>  
        [2] => 1
    )

[26] => Array
    (
        [0] => 374
        [1] => ?>
        [2] => 1
    )

)

从我读过有关token_get_all的所有内容中,我希望这些数组的[0]键成为令牌名称。我的代码/服务器发生了什么变化,我得到了这个?

我也尝试过:

$arr = token_get_all(file_get_contents('someOtherValidPHPFile.php'));

我得到了同样的结果。

我正在使用PHP版本5.4.19

2 个答案:

答案 0 :(得分:2)

是,令牌类型位于索引0上。

这只是一个标识令牌类型的数值。然后,您可以将它们与以下令牌类型列表进行比较:List of Parser Tokens

您可以使用token_name()功能获取令牌名称。

答案 1 :(得分:1)

标记定义为constants。例如。常量名为T_ARRAY,其值为362。您可以将令牌与该常量进行比较:

if ($token[0] == T_ARRAY) ...

如果您想获得可读名称,请使用token_name