Juma Alphonce
Juma Alphonce

Reputation: 41

Removing all html comments except internet explorer comments using regex and php

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.

Upvotes: 4

Views: 2439

Answers (3)

stema
stema

Reputation: 92976

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.

Upvotes: 9

Craig Sefton
Craig Sefton

Reputation: 903

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

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

Upvotes: 1

AabinGunz
AabinGunz

Reputation: 12347

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

enter image description here

Upvotes: 0

Related Questions