Removing all html comments except comments in Internet Explorer using regex and php

I am new to regex, but I need code that removes all html comments ( <!-- here --> ), but not comments in Internet Explorer, such as ( <!--[if IE 7]> here <![endif]--> ). I have this code: 369

 <?php function stripTags($text, $tags) { // replace the internet explorer comments tags so they do not get stripped $text = preg_replace("<!--[if IE7] (.*?) <![endif]-->", "#?#", $text); // replace all the normal html comments $text =preg_replace('/<!--(.|\n)*?-->/g', '', $&text); // return internet explorer comments tags to their origial place $text = preg_replace("@#\?#@", "<!--[if IE7] (.*?) <![endif]-->", $text); return $text; } ?> 

Any help please.

+4
source share
3 answers

Why not just use a negative result to ensure that the comment does not start with [if ? which is easier to read, and the comment may also contain [ and ] .

 <!--(?!\[if).*?--> 

Watch here online

Update: the lookahead statement is a non-capturing (zero-length) expression (e.g. achor \ b that checks the word boundary), it means that it does not consume characters, it checks the expression matches, and if so, it continues immediately after the character before the expression. Negative - checks if the following expression is present. I had a better link to the manual, here is PerlReTut . There must be no difference at this moment with php.

+9
source

If you know that no HTML comments on the page use [and] characters except for if conditions, you can use:

preg_replace("/<!--([^\[\]]*)-->/", "", $text);

+1
source

try it
\<!--[\(\.\|\\\w\)\*\?\d\-\+\}\{]+-->

enter image description here

0
source

All Articles