werva
werva

Reputation: 1639

preg_replace to remove IE comment

This is my string:

aaa <!--[if (gt IE 9)|!(IE)]><!--> <html lang=\"en\"> <!--<![endif]--> bbb

and this it what I want:

aaa  <html lang=\"en\">  bbb

and this is what I get:

aaa    bbb

what is wrong here?

<?php
$content="aaa <!--[if (gt IE 9)|!(IE)]><!--> <html lang=\"en\"> <!--<![endif]--> bbb";
$tagOpen="<!--[if (gt IE 9)|!(IE)]><!-->";
$tagClose="<!--<![endif]-->";
$condition='/'.preg_quote($tagOpen).'.*?'.preg_quote($tagClose).'/i';
$content=preg_replace($condition, '$1', $content);
echo htmlentities($content);

link: http://3v4l.org/TrO1U

Upvotes: 0

Views: 139

Answers (1)

Jerry
Jerry

Reputation: 71538

You didn't create a capture group. Try:

$condition='/'.preg_quote($tagOpen).'(.*?)'.preg_quote($tagClose).'/i';

Upvotes: 1

Related Questions