正则表达式剥离phpdoc多行注释

时间:2010-05-01 10:52:40

标签: regex comments phpdoc strip

我有这个:

/**
 * @file
 * API for loading and interacting with modules.
 * More explaination here.
 *
 * @author  Reveller <me@localhost>
 * @version 19:05 28-12-2008
 */

我正在寻找一个除了@token数据之外的所有正则表达式,所以结果将是:

@file API for loading and interacting with modules. More explaination here.
@author Reveller <me@localhost>
@version 19:05 28-12-2008

我现在有了这个:

$text = preg_replace('/\r?\n *\* */', ' ', $text);

它可以部分完成工作:它只删除每行前面的*。谁可以帮助我,所以它也剥离/ **和最后的斜线/?任何帮助将不胜感激!

P.S:例如,如果commentlbock包含类似

的内容
/**
 * @foo Here's some slashes for ya: / and \
 */

然后显然@foo之后的斜杠可能不会被剥离。 reult必须是:

@foo Here's some slashes for ya: / and \

我希望那里有一个正则表达式大师: - )

1 个答案:

答案 0 :(得分:4)

尝试

$result = preg_replace('%(\r?\n(?! \* ?@))?^(/\*\*\r?\n \* | \*/| \* ?)%m', ' ', $subject);

它会在每行的开头插入一个额外的空格,因此您可能希望在第二步中删除前导空格。

说明:

(\r?\n(?! \* ?@))?:如果可能,请匹配换行符,除非后面跟* @

^:断言以下匹配从行的开头

开始

(:匹配

/\*\*\r?\n \*/**<newline> *

|

\*/*/

|:或

\* ?*,可选地后跟另一个空格

):交替序列结束