您可以使用Fabien Potencier Sami(“Yet Another PHP API Documentation Generator”)开源项目中的“DocBlockParser”类。
首先,从GitHub获取Sami。
这是如何使用它的一个示例:
<?php
require_once 'Sami/Parser/DocBlockParser.php';
require_once 'Sami/Parser/Node/DocBlockNode.php';
class TestClass {
/**
* This is the short description.
*
* This is the 1st line of the long description
* This is the 2nd line of the long description
* This is the 3rd line of the long description
*
* @param bool|string $foo sometimes a boolean, sometimes a string (or, could have just used "mixed")
* @param bool|int $bar sometimes a boolean, sometimes an int (again, could have just used "mixed")
* @return string de-html_entitied string (no entities at all)
*/
public function another_test($foo, $bar) {
return strtr($foo,array_flip(get_html_translation_table(HTML_ENTITIES)));
}
}
use Sami\Parser\DocBlockParser;
use Sami\Parser\Node\DocBlockNode;
try {
$method = new ReflectionMethod('TestClass', 'another_test');
$comment = $method->getDocComment();
if ($comment !== FALSE) {
$dbp = new DocBlockParser();
$doc = $dbp->parse($comment);
echo "\n** getDesc:\n";
print_r($doc->getDesc());
echo "\n** getTags:\n";
print_r($doc->getTags());
echo "\n** getTag('param'):\n";
print_r($doc->getTag('param'));
echo "\n** getErrors:\n";
print_r($doc->getErrors());
echo "\n** getOtherTags:\n";
print_r($doc->getOtherTags());
echo "\n** getShortDesc:\n";
print_r($doc->getShortDesc());
echo "\n** getLongDesc:\n";
print_r($doc->getLongDesc());
}
} catch (Exception $e) {
print_r($e);
}
?>
这是测试页面的输出:
** getDesc:
This is the short description.
This is the 1st line of the long description
This is the 2nd line of the long description
This is the 3rd line of the long description
** getTags:
Array
(
[param] => Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => bool
[1] =>
)
[1] => Array
(
[0] => string
[1] =>
)
)
[1] => foo
[2] => sometimes a boolean, sometimes a string (or, could have just used "mixed")
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[0] => bool
[1] =>
)
[1] => Array
(
[0] => int
[1] =>
)
)
[1] => bar
[2] => sometimes a boolean, sometimes an int (again, could have just used "mixed")
)
)
[return] => Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => string
[1] =>
)
)
[1] => de-html_entitied string (no entities at all)
)
)
)
** getTag('param'):
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => bool
[1] =>
)
[1] => Array
(
[0] => string
[1] =>
)
)
[1] => foo
[2] => sometimes a boolean, sometimes a string (or, could have just used "mixed")
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[0] => bool
[1] =>
)
[1] => Array
(
[0] => int
[1] =>
)
)
[1] => bar
[2] => sometimes a boolean, sometimes an int (again, could have just used "mixed")
)
)
** getErrors:
Array
(
)
** getOtherTags:
Array
(
)
** getShortDesc:
This is the short description.
** getLongDesc:
This is the 1st line of the long description
This is the 2nd line of the long description
This is the 3rd line of the long description