Reputation: 2000
I want a regular expression which can strip all comments from the HTML using PHP.
I saw some threads on stackoverflow like Regexp match strickly html comment string, but the regex provided there doesn't work. My PHP code outputs nothing after I apply the provided code.
I have written:
$regex = array('/<!--((.*)!(\[if))-->/Uis', "/[[:blank:]]+/");
$replaced_comment_in_html = preg_replace($regex, '', $html);
But it shows comments the HTML:
<!-- This is my test comment, which I want to be removed in HTML -->
<!--[if lt IE 9]>
<script src="something.js"></script>
<![endif]-->
It does not remove the comments that I want to be removed, and if I write the below regex, then it removes all comments (also the IE style and scripts, which are required on the page)
$regex = array('/<!--(.*)-->/Uis', "/[[:blank:]]+/");
Can someone help?
Upvotes: 0
Views: 139
Reputation: 59292
Use this regex:
<!--[^\[].*-->
This will not remove IE comments, but will remove other comments.
Use it like this:
$regex = array('/<!--[^\[].*-->/Uis', "/[[:blank:]]+/");
$replaced_comment_in_html = preg_replace($regex, '', $html);
Upvotes: 1