【问题标题】:Regex to avoid stripping IE conditional tags正则表达式避免剥离 IE 条件标签
【发布时间】:2015-10-21 18:14:50
【问题描述】:

我在 PHP 中使用正则表达式剥离我的 HTML cmets。

$html = preg_replace('/<!--(.*)-->/Uis', '', $html);

但是,我不希望正则表达式删除我拥有的条件 IE TAGS

<!--[if lte IE 6]><html class="ie6 no-js">
<!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]-->
<!--[if IE]>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<![endif]-->

有什么简单的方法吗?

编辑: 为方便起见,我在这里添加了相关的 HTML 代码: http://pastebin.com/dPGHHXjU (要么) 这里:https://regex101.com/r/yS8zM6/1

提前致谢

【问题讨论】:

  • preg_replace('/&lt;!--([^&lt;&gt;]*)--&gt;/Uis', '', $html);
  • @AvinashRaj:您的解决方案不会删除任何评论,兄弟

标签: php regex


【解决方案1】:

也许this one:

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

希望对您有所帮助。

【讨论】:

  • 但您的正则表达式适用于小代码。 (您测试过的那个)。我该怎么办?这是代码:regex101.com/r/yS8zM6/1
【解决方案2】:

我从另一个帖子中得到了答案:

$html = preg_replace('~<!--(?!<!)[^\[>].*?-->~s','', $html);

这个正则表达式运行良好。 (但我不确定它的作用)

参考:https://stackoverflow.com/a/11337360/1163642(Allen Pestaluky 对该答案的评论)

我在这里添加了一个演示:https://regex101.com/r/yS8zM6/2

【讨论】:

    猜你喜欢
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 2013-10-17
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    相关资源
    最近更新 更多