token_get_all

Split given source into PHP tokens

Syntax

token_get_all ( string $code [, int $flags = 0 ] ) : array

Parameters

code

The PHP source to parse.

flags

TOKEN_PARSE Recognizes the ability to use reserved words in specific contexts.

Return

An array of token identifiers.

Each individual token identifier is either a single character (i.e.: ;, ., >, !, etc...) or a three element array containing: the token index in element 0, the string content of the original token in element 1, and the line number in element 2.

Examples

code

<?

$code = "<?
echo 'Hello';
?>";

$return = token_get_all($code);

print_r($return);

?>
Array
(
    [0] => Array
        (
            [0] => 390
            [1] => <?
            [2] => 1
        )

    [1] => Array
        (
            [0] => 393
            [1] => 

            [2] => 1
        )

    [2] => Array
        (
            [0] => 326
            [1] => echo
            [2] => 2
        )

    [3] => Array
        (
            [0] => 393
            [1] =>  
            [2] => 2
        )

    [4] => Array
        (
            [0] => 318
            [1] => 'Hello'
            [2] => 2
        )

    [5] => ;
    [6] => Array
        (
            [0] => 393
            [1] => 

            [2] => 2
        )

    [7] => Array
        (
            [0] => 392
            [1] => ?>
            [2] => 3
        )

)

flags | 0

<?

$code = "<?
class myclass
{
    const PUBLIC = 1;
}
?>";
$flags = 0;

$return = token_get_all($code, $flags);

print_r($return);

?>
Array
(
    [0] => Array
        (
            [0] => 390
            [1] => <?
            [2] => 1
        )

    [1] => Array
        (
            [0] => 393
            [1] => 

            [2] => 1
        )

    [2] => Array
        (
            [0] => 366
            [1] => class
            [2] => 2
        )

    [3] => Array
        (
            [0] => 393
            [1] =>  
            [2] => 2
        )

    [4] => Array
        (
            [0] => 311
            [1] => myclass
            [2] => 2
        )

    [5] => Array
        (
            [0] => 393
            [1] => 

            [2] => 2
        )

    [6] => {
    [7] => Array
        (
            [0] => 393
            [1] => 
    
            [2] => 3
        )

    [8] => Array
        (
            [0] => 347
            [1] => const
            [2] => 4
        )

    [9] => Array
        (
            [0] => 393
            [1] =>  
            [2] => 4
        )

    [10] => Array
        (
            [0] => 360
            [1] => PUBLIC
            [2] => 4
        )

    [11] => Array
        (
            [0] => 393
            [1] =>  
            [2] => 4
        )

    [12] => =
    [13] => Array
        (
            [0] => 393
            [1] =>  
            [2] => 4
        )

    [14] => Array
        (
            [0] => 309
            [1] => 1
            [2] => 4
        )

    [15] => ;
    [16] => Array
        (
            [0] => 393
            [1] => 

            [2] => 4
        )

    [17] => }
    [18] => Array
        (
            [0] => 393
            [1] => 

            [2] => 5
        )

    [19] => Array
        (
            [0] => 392
            [1] => ?>
            [2] => 6
        )

)

flags | TOKEN_PARSE

<?

$code = "<?
class myclass
{
    const PUBLIC = 1;
}
?>";
$flags = TOKEN_PARSE;

$return = token_get_all($code, $flags);

print_r($return);

?>
Array
(
    [0] => Array
        (
            [0] => 390
            [1] => <?
            [2] => 1
        )

    [1] => Array
        (
            [0] => 393
            [1] => 

            [2] => 1
        )

    [2] => Array
        (
            [0] => 366
            [1] => class
            [2] => 2
        )

    [3] => Array
        (
            [0] => 393
            [1] =>  
            [2] => 2
        )

    [4] => Array
        (
            [0] => 311
            [1] => myclass
            [2] => 2
        )

    [5] => Array
        (
            [0] => 393
            [1] => 

            [2] => 2
        )

    [6] => {
    [7] => Array
        (
            [0] => 393
            [1] => 
    
            [2] => 3
        )

    [8] => Array
        (
            [0] => 347
            [1] => const
            [2] => 4
        )

    [9] => Array
        (
            [0] => 393
            [1] =>  
            [2] => 4
        )

    [10] => Array
        (
            [0] => 311
            [1] => PUBLIC
            [2] => 4
        )

    [11] => Array
        (
            [0] => 393
            [1] =>  
            [2] => 4
        )

    [12] => =
    [13] => Array
        (
            [0] => 393
            [1] =>  
            [2] => 4
        )

    [14] => Array
        (
            [0] => 309
            [1] => 1
            [2] => 4
        )

    [15] => ;
    [16] => Array
        (
            [0] => 393
            [1] => 

            [2] => 4
        )

    [17] => }
    [18] => Array
        (
            [0] => 393
            [1] => 

            [2] => 5
        )

    [19] => Array
        (
            [0] => 392
            [1] => ?>
            [2] => 6
        )

)

1

<?

$code = "<?
class myclass
{
    const PUBLIC = 1;
}
?>";
$flags = 0;

$return = token_get_all($code, $flags);

foreach ($return as $value)
{
    if (is_array($value))
    {
        echo $value[2] . ": " . token_name($value[0]) . ": " . $value[1] . PHP_EOL;
    }
    else
    {
        echo $value . PHP_EOL;
    }
}

?>
1: T_OPEN_TAG: <?
1: T_WHITESPACE: 

2: T_CLASS: class
2: T_WHITESPACE:  
2: T_STRING: myclass
2: T_WHITESPACE: 

{
3: T_WHITESPACE: 
    
4: T_CONST: const
4: T_WHITESPACE:  
4: T_PUBLIC: PUBLIC
4: T_WHITESPACE:  
=
4: T_WHITESPACE:  
4: T_LNUMBER: 1
;
4: T_WHITESPACE: 

}
5: T_WHITESPACE: 

6: T_CLOSE_TAG: ?>

2

<?

$code = "<?
class myclass
{
    const PUBLIC = 1;
}
?>";
$flags = TOKEN_PARSE;

$return = token_get_all($code, $flags);

foreach ($return as $value)
{
    if (is_array($value))
    {
        echo $value[2] . ": " . token_name($value[0]) . ": " . $value[1] . PHP_EOL;
    }
    else
    {
        echo $value . PHP_EOL;
    }
}

?>
1: T_OPEN_TAG: <?
1: T_WHITESPACE: 

2: T_CLASS: class
2: T_WHITESPACE:  
2: T_STRING: myclass
2: T_WHITESPACE: 

{
3: T_WHITESPACE: 
    
4: T_CONST: const
4: T_WHITESPACE:  
4: T_STRING: PUBLIC
4: T_WHITESPACE:  
=
4: T_WHITESPACE:  
4: T_LNUMBER: 1
;
4: T_WHITESPACE: 

}
5: T_WHITESPACE: 

6: T_CLOSE_TAG: ?>

Internal

token_name

External

PHP

Home Menu