PHP preg_replace 带有空白空间的 html 注释

标签 php html regex comments 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/

相关文章:

php - 通过url防止xss攻击(PHP)

html - 如何用flex水平显示hr?

c# - 在 C# 正则表达式中,为什么初始匹配出现在组中?

PHPUnit TDD,PHP fatal error : Call to undefined method

javascript - Laravel 验证表中的唯一值以放入错误函数(js)

php - 如何将json数据传递到页面?

html - 我网站上AdSense图片广告下的空白问题

javascript - 如何强制移动浏览器为输入类型 ="text"拉出数字键盘?

c# - 正则表达式 if 条件 c#

python - 两个正则表达式类的交集