1 html php regex comments preg-replace
我有一些像这样的PHP代码:
$test = "<!--my comment goes here--> Hello World";
Run Code Online (Sandbox Code Playgroud)
现在我想从字符串中删除整个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";
Run Code Online (Sandbox Code Playgroud)
产量
$ php test.php
Hello World"
blah < blah2
end
Run Code Online (Sandbox Code Playgroud)
或者如果你必须preg_replace,
preg_replace("/<!--.*?-->/ms","",$str);
Run Code Online (Sandbox Code Playgroud)
小智 6
preg_replace('/<!--(.*)-->/Uis', '', $html)
Run Code Online (Sandbox Code Playgroud)
将删除$html字符串中包含的每个html注释.希望这可以帮助!