2

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...

denlau
  • 916
  • 2
  • 9
  • 21
  • Related: [Regex to strip comments and multi-line comments and empty lines](http://stackoverflow.com/questions/643113/regex-to-strip-comments-and-multi-line-comments-and-empty-lines) – bansi Jul 04 '13 at 11:38

1 Answers1

1

I would use something like this

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

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

Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • This: /(?s)(?<=\r?\n|^)###.*?(?<=\r?\n)###.*?(?=\r?\n|$)/ gives the following error: Compilation failed: lookbehind assertion is not fixed length at offset 13 – denlau Jul 04 '13 at 12:10
  • try without using lookbehind `(?s)(\r?\n|^)###.*?(\r?\n)###.*?(?=\r?\n|$)` – Anirudha Jul 04 '13 at 12:11
  • That works - i've also found another version, that also worked - if the static parts is used at everytime. I'm going to add the answers to my question :) Thanks a lot! – denlau Jul 04 '13 at 12:13