gpt4 book ai didi

php - 如何用 PHP 解析 phpDoc 风格的注释 block ?

转载 作者:可可西里 更新时间:2023-10-31 22:49:20 25 4
gpt4 key购买 nike

请考虑以下代码,我试图用它仅解析文件中的第一个 phpDoc 样式注释(不使用任何其他库)(文件内容放入 $data 变量用于测试目的):

$data = "
/**
* @file A lot of info about this file
* Could even continue on the next line
* @author me@example.com
* @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() 注释 block 和代码)被认为是@ 的值待办事项:

Array (
[file] => A lot of info about this file Could even continue on the next line
[author] => me@example.com
[version] => 2010-05-01
[todo] => do stuff... /

/** Comment bij functie bar()
[param] => Array met dingen /
function bar() {
echo ;
}
)

我的代码需要如何更改才能只解析第一个注释 block (换句话说:解析应该在遇到第一个“*/”后停止?

最佳答案

使用 PCRE 编写解析器会给您带来麻烦。我建议依靠 tokenizerreflection第一的。那么实际为 doc block 实现一个解析器会更安全,它可以处理 phpdoc 支持的所有情况。格式(所有库也结束了)。

关于php - 如何用 PHP 解析 phpDoc 风格的注释 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2749301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com