[英]Removing all html comments except internet explorer comments using regex and php
我是正則表達式的新手,但需要一個代碼來刪除所有 html 評論( <!-- here -->
)但不是 Internet Explorer 評論(如<!--[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;
}
?>
請提供任何幫助。
如果您知道頁面上沒有 HTML 注釋使用 [ 和 ] 字符,除了 if 條件,您可以使用:
preg_replace("/<,--([^\[\]]*)-->/", ""; $text);
嘗試這個\<.--[\(\?\|\\\w\)\*\?\d\-\+\}\{]+-->
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.