如何在PHP中删除html注释 [英] how to remove html comments in php

查看:121
本文介绍了如何在PHP中删除html注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除嵌入html文件的任何注释

I am trying to remove any comments embedded with the html file

$data= file_get_contents($stream); <br>
$data = preg_replace('<!--*-->', '', $data); <br>
echo $data;

我还是收到所有的评论< ! - bla bla bla - >

我做错了什么?

I am still ending up with all the comments < !- bla bla bla -->
What am I doing wrong?

推荐答案

不使用正则表达式:

function strip_comments($html){
$html = str_replace(array("\r\n<!--", "\n<!--"), "<!--", $html);
    while(($pos = strpos($html, "<!--")) !== false){
    if(($_pos = strpos($html, "-->", $pos)) === false)
    $html = substr($html, 0, $pos);
    else
    $html = substr($html, 0, $pos).substr($html, $_pos+3);
    }
return $html;
}

这篇关于如何在PHP中删除html注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆