I am new in regex but need a code that would remove all html comments (<!-- here -->
) but not internet explorer comments like (<!--[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.
Why not just use a negative lookahead to ensure that the comment does not start with [if
? that is easier to read and the comment can contain also [
and ]
.
<!--(?!\[if).*?-->
See here online
Update: A lookahead assertion is a non capturing(zero length) expression (like an achor \b that checks for a word boundary), that means it does not consum the characters, it checks if the expression is matching and if yes it continues right after the character before the expression. The negative one is checking that there isn't the expression following. I better link to a manual, here is the PerlReTut. Should be at that point no difference to php.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With