使用正则表达式和 php 删除除 Internet Explorer 注释之外的所有 html 注释

2024-04-05

我是正则表达式新手,但需要一个可以删除所有 html 注释的代码(<!-- here -->)但不是像(<!--[if IE 7]> here <![endif]-->)。我有这个代码: 第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;
}
?>

请提供任何帮助。


为什么不直接使用否定前瞻来确保评论不以[if?这更容易阅读,评论也可以包含[ and ].

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

在线查看这里 http://www.rubular.com/r/aHoURowKFu

更新:前瞻断言是一个非捕获(零长度)表达式(就像检查单词边界的 achor \b ),这意味着它不消耗字符,它检查表达式是否匹配,如果是,则继续正确在表达式之前的字符之后。消极的一面是检查后面是否存在表达式。我最好链接到手册,这是佩尔·雷图特 http://perldoc.perl.org/perlretut.html#Looking-ahead-and-looking-behind。到那时应该与 php 没有区别。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用正则表达式和 php 删除除 Internet Explorer 注释之外的所有 html 注释 的相关文章

随机推荐