-1

If I want to remove

<!-- bof_foo --> 
some content 
<!-- eof_foo -->

from a string, what is the pattern parameter for preg_repl? I would have thought that

$pattern = "/<!-- bof_foo -->[.\n]*<!-- eof_foo -->/";

would work, but it didn't.

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
  • 1
    See [this answer](https://stackoverflow.com/a/21574655/3832970). Note that `[.\n]` matches a dot and a newline. You may use `"/.*?/s"` – Wiktor Stribiżew Jun 13 '17 at 16:06
  • 1
    Change `[.\n]*` to `[\S\s]*?` and you're good to go. To @WiktorStribiżew: This is not a duplicate of [RegExp to strip HTML comments](https://stackoverflow.com/questions/1084741/regexp-to-strip-html-comments). There, the question is how to only strip _any_ comment. This question is how to remove a fixed _from comment all the way to a to comment_. –  Jun 13 '17 at 16:33
  • Don't ever use this `` to match html comments. As was one of the versions of the accepted answer in that so called duplicate. That won't match something like `>>-->` –  Jun 13 '17 at 16:39
  • @sln Please convert your comment to an answer so I can give you the points. Thank you! – Scott C Wilson Jun 13 '17 at 16:43
  • @Scott There is no need to use `[\s\S]` to match any char in a PHP PCRE regex. `[\s\S]` is a hack used in those languages where DOTALL modifier is not supported (as JavaScript `RegExp`). There is a DOTALL `s` modifier that redefines `.` behavior. Your question is marked as a dupe because the answer I linked to provides the exact regex you need. – Wiktor Stribiżew Jun 13 '17 at 16:43
  • Thanks @WiktorStribiżew. I see your point, but I felt it was a bit different because the question was different and the answer you pointed to was not the accepted answer to the question. With the accepted answer, this might help searchers in the future. – Scott C Wilson Jun 13 '17 at 16:51
  • **Dupe of [RegExp to strip HTML comments](https://stackoverflow.com/questions/1084741/regexp-to-strip-html-comments). Also, a dupe of [Get content between two strings PHP](https://stackoverflow.com/questions/1445506/get-content-between-two-strings-php)** – Wiktor Stribiżew Jun 13 '17 at 16:55

1 Answers1

0

Change this

$pattern = "/<!-- bof_foo -->[.\n]*<!-- eof_foo -->/";  

to

$pattern = "/<!-- bof_foo -->[\S\s]*?<!-- eof_foo -->/";  

and it will work.