我想剔除文本中的 <!-- -->注释块中的所有代码,但是多行注释的搞不掉,能否帮我看看怎么才能搞掉?
<?php
$sTmp = '
<!--scroll
menu-->
<SCRIPT language=javascript>
<!--scroll menu-->
<!--
if (isNS4) {
}
//-->
</SCRIPT>
';
$aRules = '/<!--(?:.(?<!<--))*.*-->$/m';
print_r(preg_replace($aRules, '', $sTmp));
?>
<?php
$sTmp = '
<!--scroll
menu-->
<SCRIPT language=javascript>
<!--scroll menu-->
<!--
if (isNS4) {
}
//-->
</SCRIPT>
';
$aRules = '/<!--(?:.(?<!<--))*.*-->$/m';
print_r(preg_replace($aRules, '', $sTmp));
?>
剔除后是这个结果。
<!--scroll
menu-->
<SCRIPT language=javascript> <!--
if (isNS4) {
}
//-->
</SCRIPT>
如果设定了此修正符,模式中的圆点元字符(.)匹配所有的字符,包括换行符。没有此设定的话,则不包括换行符。这和 Perl 的 /s 修正符是等效的。排除字符类例如 [^a] 总是匹配换行符的,无论是否设定了此修正符。
====================================
$sTmp = '
<!--scroll
menu-->
<SCRIPT language=javascript>
<!--scroll menu-->
<!--
if (isNS4) {
}
//-->
</SCRIPT>
';
$aRules = '/<!--(.*?)-->/is';
print_r(preg_replace($aRules, '', $sTmp));
查找:<!--[]*-->
替换为:
假设原始文件为:
<?php
$sTmp = '
<!--scroll
menu-->
<SCRIPT language=javascript>
<!--scroll menu-->
<!--
if (isNS4) {
}
//-->
</SCRIPT>
';
$aRules = '/<!--(?:.(?<!<--))*.*-->$/m';
print_r(preg_replace($aRules, '', $sTmp));
?>
将被转换为:
<?php
$sTmp = '
<SCRIPT language=javascript>
</SCRIPT>
';
$aRules = '/$/m';
print_r(preg_replace($aRules, '', $sTmp));
?>