This question already has an answer here:
- RegExp to strip HTML comments 11 answers
I am getting page contents into variable $content
I need to strip HTML comments from $content using regular expression. I tried following code, it's not working properly
$content = preg_replace('/<!--(.|\)*?-->/', '', $content);
Use this:
you have to escape
!
because it's part of reg exp and also need to include new liness
modifier, this for if comment is not one line. And lazy flagU
to match as less as possible, this when you got multiple comments Works perfectlooks like you are missing something.
You can test it here http://www.phpliveregex.com/p/1LX
Your back slash is escaping your
)
... I'm not sure what you think(.|\)
is doing; Why not just use.*?
and remove the capture group entirely?Also, you want to set the
s
modifier to make.
match new lines.Revised code
http://php.net/manual/en/reference.pcre.pattern.modifiers.php
http://www.regular-expressions.info/