請考慮下面的代碼與我試圖解析只有第一個PHPDoc的風格註釋在一個文件中(不使用任何其他庫)(放在$數據變量用於測試文件的內容):如何使用PHP解析phpDoc樣式註釋塊?
$data = "
/**
* @file A lot of info about this file
* Could even continue on the next line
* @author [email protected]
* @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]
[version] => 2010-05-01
[todo] => do stuff.../
/** Comment bij functie bar()
[param] => Array met dingen/
function bar() {
echo ;
}
)
如何確實需要我的代碼被改變,使得只有Ť他正在解析第一個註釋塊(換句話說:在遇到第一個「* /」之後解析應該停止?
考慮一下s字符串像'$ s ='/ **不是phpDoc @file ... * /';'放在第一個phpDoc之前的情況。換句話說:使用正則表達式,您將獲得100%可靠的解決方案。 – 2010-05-01 09:01:23