<分区>
我有一些这样的 php 代码:
$test = "<!--my comment goes here--> Hello World";
现在我想从字符串中删除整个 html 注释,我知道我需要使用 preg_replace,但现在确定要在正则表达式中使用它。 有人可以帮忙吗? 谢谢
<分区>
我有一些这样的 php 代码:
$test = "<!--my comment goes here--> Hello World";
现在我想从字符串中删除整个 html 注释,我知道我需要使用 preg_replace,但现在确定要在正则表达式中使用它。 有人可以帮忙吗? 谢谢
最佳答案
$str=<<<'EOF'
<!--my comment goes here--> Hello World"
blah <!-- my another
comment here --> blah2
end
EOF;
$r="";
$s=explode("-->",$str);
foreach($s as $v){
$m=strpos($v,'<!--');
if($m!==FALSE){
$r.=substr($v,1,$m);
}
}
$r.=end($s);
print $r."\n";
输出
$ php test.php
Hello World"
blah < blah2
end
或者如果你必须preg_replace,
preg_replace("/<!--.*?-->/ms","",$str);
关于PHP preg_replace 带有空白空间的 html 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2499065/