0

我需要获取 php 类中特定函数的注释,例如:

/* Some commets for a class */
class Foo extends Bar {
    function __construct() {}

    // a single line comment to the function foo()
    function foo() {}

    /* a multi-line comment
    to the function bar() */
    public static function bar() {}

}

是的,我知道,这可以通过 ReflectionMethod->getDocComment() 轻松完成,但它对我不起作用,因为我使用 eAccelerator 并且它会从代码中删除所有注释,因此 getDocComment 总是返回 FALSE。

我也不想重新编译 eAccelerator :)

我需要这样的功能:

function get_function_comment($class_contents, $function_name) {}

所以我会返回一个函数的注释,$class_contents 是一个变量,它存储类内容,如上例所示。

我尝试自己做,但我无法创建正确的正则表达式..

请帮我 :)

4

2 回答 2

1

哦,伙计,我几乎觉得写这个正则表达式很脏,但这可能会奏效(还没有测试过,所以不要相信我的话)。

preg_match('#(//.*$|/\*.*\*/)\s*$[\s\w]*function\s+$function_name\b#Usmi', $class_contents, $result);

理论上,它的工作原理如下:

  • 查找:
    • //以及直到该行末尾的所有内容或
    • /*, 然后一切直到 */
  • 然后吃掉所有的空格直到行尾
  • 在下一行取任意数量的空格或单词字符,直到你点击“function”,一些空格,然后是你想要的整个函数名。

在实践中:“有些人在遇到问题时会想‘我知道,我会使用正则表达式。’现在他们有两个问题。”

于 2011-10-19T16:10:35.973 回答
0

尝试使用正确的 phpDoc 注释:

/**
 * Something explaining this
 *
 * @return string
 */
function foo(){  }
于 2011-10-19T15:41:30.060 回答