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);
looks like you are missing something.
$content = preg_replace( '/<!--(.|\s)*?-->/' , '' , $content );
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
$content = preg_replace('/<!--.*?-->/s', '', $content);
http://php.net/manual/en/reference.pcre.pattern.modifiers.php
http://www.regular-expressions.info/
Use this:
you have to escape !
because it's part of reg exp and also need to include new lines s
modifier, this for if comment is not one line. And lazy flag U
to match as less as possible, this when you got multiple comments
Works perfect
$content = preg_replace('/<\!--.*-->/Us', '', $content);