6

I have this:

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

I'm looking for a regex to strip all but the @token data, so the result would be:

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

I now have this:

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

It does the job partially: it only removes the * in front of each line. Who could help me so it also strips /** and the final slash /? Any help would be greatly appreciated!

P.S: If, for instance, the commentlbock would contain something like

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

Then obviously the slashes after @foo may not be stripped. The reult would have to be:

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

I hope there's a regex guru out there :-)

1 Answer 1

4

Try

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

It will insert an extra space at the start of each line, so you might want to strip leading whitespace in a second step.

Explanation:

(\r?\n(?! \* ?@))?: If possible, match a newline unless it's followed by * @

^: Assert that the following match starts at the beginning of the line

(: Either match

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

| or

\*/: */

|: or

\* ?: *, optionally followed by another space

): End of alternation sequence

Sign up to request clarification or add additional context in comments.

3 Comments

Wow, thanks! It works like a charm :-) Trimming whitespace is no problem.
@Reveller, if this solved your problem, you should mark the answer as accepted, and perhaps upvote it as well.
Done and done :-) Thanks for telling me. I'm new here and didn't know you could do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.