我目前正在使用PHP和正则表达式去除页面中的所有HTML注释。脚本效果很好......有点太好了。它删除了所有评论,包括我的条件评论。这就是我所拥有的:

<?php
  function callback($buffer)
  {
        return preg_replace('/<!--(.|\s)*?-->/', '', $buffer);
  }

  ob_start("callback");
?>
... HTML source goes here ...
<?php ob_end_flush(); ?>

由于我的正则表达式不是太热,我无法弄清楚如何修改模式以排除条件注释,例如:

<!--[if !IE]><!-->
<link rel="stylesheet" href="/css/screen.css" type="text/css" media="screen" />
<!-- <![endif]-->

<!--[if IE 7]>
<link rel="stylesheet" href="/css/ie7.css" type="text/css" media="screen" />
<![endif]-->

<!--[if IE 6]>
<link rel="stylesheet" href="/css/ie6.css" type="text/css" media="screen" />
<![endif]-->

干杯

有帮助吗?

解决方案

由于评论不能嵌套在HTML中,理论上正则表达式可以完成这项工作。尽管如此,使用某种解析器将是更好的选择,特别是如果您的输入不能保证格式良好。

这是我的尝试。要仅匹配正常注释,这将起作用。它已成为一个巨大的怪物,对不起。我已经对它进行了相当广泛的测试,它似乎做得很好,但我不做任何保证。

<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->

说明:

<!--                #01: "<!--"
(?!                 #02: look-ahead: a position not followed by:
  \s*               #03:   any number of space
  (?:               #04:   non-capturing group, any of:
    \[if [^\]]+]    #05:     "[if ...]"
    |<!             #06:     or "<!"
    |>              #07:     or ">"
  )                 #08:   end non-capturing group
)                   #09: end look-ahead
(?:                 #10: non-capturing group:
  (?!-->)           #11:   a position not followed by "-->"
  .                 #12:   eat the following char, it's part of the comment
)*                  #13: end non-capturing group, repeat
-->                 #14: "-->"

步骤#02和#11至关重要。 #02确保以下字符不表示条件注释。之后,#11确保以下字符不表示评论的结束,而#12和#13导致实际匹配。

申请“全球”和“dotall”标志。

要做相反的事情(仅匹配条件评论),它将是这样的:

<!(--)?(?=\[)(?:(?!<!\[endif\]\1>).)*<!\[endif\]\1>

说明:

<!                  #01: "<!"
(--)?               #02: two dashes, optional
(?=\[)              #03: a position followed by "["
(?:                 #04: non-capturing group:
  (?!               #05:   a position not followed by
    <!\[endif\]\1>  #06:     "<![endif]>" or "<![endif]-->" (depends on #02)
  )                 #07:   end of look-ahead
  .                 #08:   eat the following char, it's part of the comment
)*                  #09: end of non-capturing group, repeat
<!\[endif\]\1>      #10: "<![endif]>" or "<![endif]-->" (depends on #02)

再次申请“全球”和“dotall”标志。

步骤#02是因为“下层揭示”而导致的。语法,请参阅:“MSDN - 关于条件评论”

我不完全确定允许或预期的空间。在适当的表达式中添加 \ s *

其他提示

如果您无法使用一个正则表达式,或者您发现要保留更多注释,可以使用 <代码> preg_replace_callback 。然后,您可以定义一个单独处理注释的函数。

<?php
function callback($buffer) {
    return preg_replace_callback('/<!--.*-->/U', 'comment_replace_func', $buffer);
}

function comment_replace_func($m) {
    if (preg_match( '/^\<\!--\[if \!/i', $m[0])) {
        return $m[0];   
    }              

    return '';
}   

ob_start("callback");
?>

... HTML source goes here ...

<?php ob_end_flush(); ?>

总之,这似乎是最好的解决方案:

<?php
  function callback($buffer) {
    return preg_replace('/<!--[^\[](.|\s)*?-->/', '', $buffer);
  }
  ob_start("callback");
?>
... HTML source goes here ...
<?php ob_end_flush(); ?>

它删除所有注释并离开条件,但最上面的条件除外:

<!--[if !IE]><!-->
<link rel="stylesheet" href="/css/screen.css" type="text/css" media="screen" />
<!-- <![endif]-->

其中附加似乎导致问题。

如果有人可以建议考虑到这一点的正则表达式并将该条件留在原处那么那将是完美的。

Tomalak的解决方案看起来不错,但作为一个新手,没有进一步的指导方针,我不知道如何实施它,虽然我想尝试一下,如果有人可以详细说明如何应用它?

由于

我不确定PHP的正则表达式引擎是否会喜欢以下内容,但请尝试以下模式:

'/<!--(.|\s)*(\[if .*\]){0}(.|\s)*?-->/'

这样的事可能有用:

/<!--[^\[](.|\s)*?-->/

它与你的相同,只是它忽略了注释在注释开始标记之后有一个左括号。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top