1
我正在创建一个调试/ doc脚本,我想忽略所有的标准注释,但留下PHPDoc的机智。清除所有评论,但离开PHPDoc
我使用的代码是:
// Get file into an array line by line
$line = file($file);
// Trim all empty space and # or // comments
array_map(function($v) {
$v = trim($v);
$v = preg_split("/\/\/|#/", $v)[0];
return $v;
}, $line);
是问题,这将仍留在多线C风格的注释,如:
/*
This
means
nothing
*/
因此,我相信我会读取整个文件,找到每个/*
,同时忽略/**
(PHPDoc)并删除所有内容,直到*/
。
所以正则表达式需要是多行的。我接受使用file()
的解决方案,但我认为file_get_content()
在单独的行上找到C风格的开放和关闭评论标签会更实用。
试试这个 - 反射如果它的OOP。 http://stackoverflow.com/questions/22103019/php-reflection-get-constants-doc-comment,和http://php.net/manual/en/reflectionclass.getdoccomment.php – ArtisticPhoenix
@ArtisiticPhoenix,谢谢。这看起来像使用类/ token_get_all的一个很好的起点,就像akond所建议的那样。 – Devon
是的,我宁愿使用对token_get_all的反射仅仅因为它增加了一些抽象,但我认为只能使用token_get_all来完成,或者regx的你可以用regx和简单的词法分析器类型设置来做同样的事情,但这是一个整个其他话题lol – ArtisticPhoenix