5

我有这个:

/**
 * @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);

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

PS:例如,如果 commentlbock 将包含类似

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

那么显然@foo 之后的斜线可能不会被剥离。结果必须是:

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

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

4

1 回答 1

4

尝试

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

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

解释:

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

^: 断言下一个匹配从行首开始

(: 任意匹配

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

|或者

\*/*/

|: 或者

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

): 交替序列结束

于 2010-05-01T11:02:02.213 回答