我正在做一些自制的自动化文档,因为我有一个布局不是很标准的代码库,我想知道最好的方法是读取 PHP 文件并获取注释块的内容。我能想到的唯一方法是打开文件并逐行读取它,但我认为也许有一些内置的魔法可以为我解析文档,类似于反射函数。
每个文件的基本布局是这样的:
<?php // $Id$
/**
* Here is this script's documentation, with information in pseudo-javadoc
* type tags and whatnot.
*
* @attr something some information about something
* @attr etc etc etc
*/
// rest of the code goes here.
请务必注意,这些文件中没有定义任何函数或类。这些评论与整个脚本有关。
查看分词器 http://www.php.net/manual/en/book.tokenizer.php.
获取名为的文件中的所有评论test.php
你会这样做:
$tokens = token_get_all(file_get_contents("test.php"));
$comments = array();
foreach($tokens as $token) {
if($token[0] == T_COMMENT || $token[0] == T_DOC_COMMENT) {
$comments[] = $token[1];
}
}
print_r($comments);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)