Regex for phpdoc multi-line comment

I have it:

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

I am looking for a regex to delete everything except @token data, so the result will look like this:

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

Now I have this:

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

Performs this task partially: it deletes only * before each line. Who could help me is the strips / **, and the last slash? Any help would be greatly appreciated!

PS: If, for example, commentlbock contains something like

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

Then, obviously, slashes after @foo cannot be deleted. The result should be:

 @foo Here some slashes for ya: / and \ 

I hope there is a regular expression guru there :-)

+4
source share
1 answer

Try

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

Additional space will be added at the beginning of each line, so you may need to remove leading spaces in the second step.

Explanation:

(\r?\n(?! \* ?@ ))? : If possible, match a new line, if not follow * @

^ : Make sure at the beginning of the line

The following match begins:

( : match

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

| or

\*/ : */

| : or

\* ? : * , optionally followed by a space

) : End interleaving sequence

+4
source

Source: https://habr.com/ru/post/1308533/


All Articles