Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

regex

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.

like image 350
Juma Alphonce Avatar asked Dec 28 '22 21:12

Juma Alphonce


1 Answers

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.

like image 113
stema Avatar answered Dec 30 '22 10:12

stema