Using Regular Expression remove HTML comments from

2019-05-28 22:49发布

This question already has an answer here:

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

3条回答
唯我独甜
2楼-- · 2019-05-28 23:05

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);
查看更多
smile是对你的礼貌
3楼-- · 2019-05-28 23:11

looks like you are missing something.

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

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

查看更多
劫难
4楼-- · 2019-05-28 23:21

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/

查看更多
登录 后发表回答