Using Regular Expression remove HTML comments from

2019-05-28 22:44发布

问题:

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);

回答1:

looks like you are missing something.

 $content = preg_replace( '/<!--(.|\s)*?-->/' , '' , $content );

You can test it here http://www.phpliveregex.com/p/1LX



回答2:

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/



回答3:

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);