百科狗-知识改变命运!
--

token_get_all() - Zend引擎Tokenizer

梵高1年前 (2023-11-21)阅读数 11#技术干货
文章标签代号

token_get_all()

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

将提供的源码按 PHP 标记进行分割

说明

token_get_all(string $source): array

token_get_all() - Zend引擎Tokenizer

token_get_all()解析提供的$source源码字符,然后使用 Zend 引擎的语法分析器获取源码中的 PHP 语言的解析器代号

解析器代号列表见解析器代号列表,或者使用token_name()翻译获取这个代号的字符串表示.

参数

$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.

范例

Example #1token_get_all()examples

 tags in a normal file. */
$tokens = token_get_all('/* comment */'); // => array(array(T_INLINE_HTML, '/* comment */'));
?>

更新日志

版本说明
5.2.2Line numbers are returned in element 2
Yes, some problems (On WAMP, PHP 5.3.0 ) with get_token_all() 
1 : bug line numbers
 Since PHP 5.2.2 token_get_all() should return Line numbers in element 2..
.. but for instance (5.3.0 on WAMP), it work perfectly only with PHP code (not HMTL miwed), but if you have some T_INLINE_HTML detected by token_get_all() , sometimes you find wrongs line numbers (return next line)... :(
2: bug warning message can impact loops
Warning with php code uncompleted (ex : php code line by line) :
for example if a comment tag is not closed token_get_all() can block loops on this warning :
Warning: Unterminated comment starting line
This problem seem not occur in CLI mod (php command line), but only in web mod.
Waiting more stability, used token_get_all() only on PHP code (not HMTL miwed) :
First extract entirely PHP code (with open et close php tag), 
Second use token_get_all() on the pure PHP code.
3 : Why there not function to extract PHP code (to extract HTML, we have Tidy..)?
Waiting, I used a function :
The code at end this post :
http://www.developpez.net/forums/d786381/php/langage/
fonctions/analyser-fichier-php-token_get_all/
This function not support :
- Old notation : "" and ""
- heredoc syntax 
- nowdoc syntax (since PHP 5.3.0)
I wanted to use the tokenizer functions to count source lines of code, including counting comments. Attempting to do this with regular expressions does not work well because of situations where /* appears in a string, or other situations. The token_get_all() function makes this task easy by detecting all the comments properly. However, it does not tokenize newline characters. I wrote the below set of functions to also tokenize newline characters as T_NEW_LINE.

Example usage:

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)