token_get_all()
token_get_all() — Split given source into PHP tokens.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
token_get_all() parses the given code string into PHP language tokens using the Zend engine's lexical scanner.
Signature
token_get_all(string $code, int $flags = 0): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $code | string | The PHP source to parse. |
| $flags | int | Valid flags: TOKEN_PARSE - Recognises the ability to use reserved words in specific contexts. |
Return value
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.
Example
A runnable snippet:
$tokens = token_get_all('<?php echo; ?>'); foreach ($tokens as $token) { if (is_array($token)) { echo "Line {$token[2]}: ", token_name($token[0]), " ('{$token[1]}')", PHP_EOL; } }
Output:
Line 1: T_OPEN_TAG ('<?php ') Line 1: T_ECHO ('echo') Line 1: T_WHITESPACE (' ') Line 1: T_CLOSE_TAG ('?>')
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page token_get_all() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.