PHP删除除注释外的所有html

PHP删除除注释外的所有html,php,html,comments,Php,Html,Comments,如何删除所有html输入,但不删除注释?例如: 您好 会变成这样:这个 我该怎么做?谢谢 编辑:我知道你可以用正则表达式做这样的事情,但我不知道怎么做。这确实有点复杂,但用正则表达式是可行的: $text = preg_replace('~<(?!!--)/?\w[^>]*(?<!--)>~', "", $text); $text=preg_replace('~]*(?$foo=“hello”; 预匹配('/(\)/m',$foo,$result); 打印(结果); 我

如何删除所有html输入,但不删除注释?例如: 您好

会变成这样:这个

我该怎么做?谢谢


编辑:我知道你可以用正则表达式做这样的事情,但我不知道怎么做。

这确实有点复杂,但用正则表达式是可行的:

$text = preg_replace('~<(?!!--)/?\w[^>]*(?<!--)>~', "", $text);
$text=preg_replace('~]*(?
$foo=“hello

”; 预匹配('/(\)/m',$foo,$result); 打印(结果);
我将使用以下方法提取所有注释,而不是替换HTML:

preg_match_all('#(<!--.*?-->)#s', '<html><body><!-- hello paragraph --><p>hello</p></body></html>', $m);
preg#u match_all(“#()#s”,“你好”

,$m);
我相信这种方法比试图识别非注释更可靠。它也不会有注释中的HTML标记被咀嚼或删除的缺点。
$foo="<html><body><!-- hello paragraph --><p>hello</p></body></html>";
preg_match('/(\<|<)!--(\s*.*?\s*)--(\>|>)/m',$foo,$result);
print_r($result);
preg_match_all('#(<!--.*?-->)#s', '<html><body><!-- hello paragraph --><p>hello</p></body></html>', $m);