token_get_all

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

token_get_all將提供的源碼按 PHP 標(biāo)記進(jìn)行分割

說明

token_get_all(string $source): array

token_get_all() 解析提供的 source 源碼字符,然后使用 Zend 引擎的語法分析器獲取源碼中的 PHP 語言的解析器代號

解析器代號列表見解析器代號列表, 或者使用 token_name() 翻譯獲取這個(gè)代號的字符串表示.

參數(shù)

source

需要解析的 PHP 源碼.

返回值

An array of token identifiers. Each individual token identifier is either a single character (i.e.: ;, ., 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.

范例

示例 #1 token_get_all() examples

<?php
$tokens 
token_get_all('<?php echo; ?>'); /* => array(
                                                  array(T_OPEN_TAG, '<?php'), 
                                                  array(T_ECHO, 'echo'),
                                                  ';',
                                                  array(T_CLOSE_TAG, '?>') ); */

/* Note in the following example that the string is parsed as T_INLINE_HTML
   rather than the otherwise expected T_COMMENT (T_ML_COMMENT in PHP <5).
   This is because no open/close tags were used in the "code" provided.
   This would be equivalent to putting a comment outside of <?php ?> tags in a normal file. */
$tokens token_get_all('/* comment */'); // => array(array(T_INLINE_HTML, '/* comment */'));
?>

更新日志

版本 說明
5.2.2 Line numbers are returned in element 2