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

Pr0*_*0no 5 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
 */
Run Code Online (Sandbox Code Playgroud)

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

@file API for loading and interacting with modules. More explaination here.
@author Reveller <me@localhost>
@version 19:05 28-12-2008
Run Code Online (Sandbox Code Playgroud)

我现在有这个:

$text = preg_replace('/\r?\n *\* */', ' ', $text);
Run Code Online (Sandbox Code Playgroud)

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

PS:例如,如果commentlbock包含类似的东西

/**
 * @foo Here's some slashes for ya: / and \
 */
Run Code Online (Sandbox Code Playgroud)

那么很明显@foo之后的斜杠可能不会被剥离.reult必须是:

@foo Here's some slashes for ya: / and \
Run Code Online (Sandbox Code Playgroud)

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

Tim*_*ker 4

尝试

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

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

解释:

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

^: 断言后面的匹配从行首开始

(: 要么匹配

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

|或者

\*/:*/

|: 或者

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

):交替序列结束