denlau
denlau

Reputation: 1016

Remove multi-line comment from string

I want to make it possible to write some comments in my view files, that is removed before it is send to the user's browser.

An example of what the comments could be, is:

### - [[VIEW COMMENTS
# Comments for a view file.
#
### - VIEW COMMENTS END]]

Where the static parts is line 1 and line 4 - and everything in between is variable.

How should I do that? I've tried with preg_replace, but it was not successfull:

$build = preg_replace("/([### - \[\[VIEW COMMENTS])([^\[\]+)([### - VIEW COMMENTS END\]\]])", '', $build);

The answer is (made by Anirudh):

$build = preg_replace('/(?s)(\r?\n|^)###.*?(\r?\n)###.*?(?=\r?\n|$)/', '', $build);

and I also made this work myself:

$build = preg_replace('/([### - [[VIEW COMMENTS])(.*)([### - VIEW COMMENTS END]]])/s', '', $build);

Thanks...

Upvotes: 2

Views: 332

Answers (1)

Anirudha
Anirudha

Reputation: 32827

I would use something like this

(?s)(?<=\r?\n|^)###.*?(?<=\r?\n)###.*?(?=\r?\n|$)

(?s) toggles the single mode enabling . to match newlines

Upvotes: 1

Related Questions