请考虑以下代码,我尝试仅解析文件中的第一个 phpDoc 样式注释(不使用任何其他库)(出于测试目的,将文件内容放入 $data 变量中):
$data = "
/**
* @file A lot of info about this file
* Could even continue on the next line
* @author [email protected] /cdn-cgi/l/email-protection
* @version 2010-05-01
* @todo do stuff...
*/
/**
* Comment bij functie bar()
* @param Array met dingen
*/
function bar($baz) {
echo $baz;
}
";
$data = trim(preg_replace('/\r?\n *\* */', ' ', $data));
preg_match_all('/@([a-z]+)\s+(.*?)\s*(?=$|@[a-z]+\s)/s', $data, $matches);
$info = array_combine($matches[1], $matches[2]);
print_r($info)
这几乎有效,除了以下事实:一切@todo 之后(包括bar()
注释块和代码)被认为是的值@todo
:
Array (
[file] => A lot of info about this file Could even continue on the next line
[author] => [email protected] /cdn-cgi/l/email-protection
[version] => 2010-05-01
[todo] => do stuff... /
/** Comment bij functie bar()
[param] => Array met dingen /
function bar() {
echo ;
}
)
我的代码需要如何更改,以便仅解析第一个注释块(换句话说:解析应该在遇到第一个“*/”之后停止?
使用 PCRE 编写解析器会给你带来麻烦。我建议依靠分词器 http://www.php.net/tokenizer or 反射 http://www.php.net/manual/en/reflectionclass.getdoccomment.php第一的。那么实际为 doc 块实现一个解析器会更安全,它可以处理 doc 块支持的所有情况phpdoc http://www.phpdoc.org/格式(所有库最终都要做的事情)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)