防止解析评论

时间:2011-04-06 09:57:11

标签: php html browser

有没有办法在我的php / html文件中隐藏评论?

我想添加标记,我不希望人们能够在浏览器中查看源代码。

这可能吗?

<!-- Prevent this comment from being viewed -->
<?php...

提前致谢。

3 个答案:

答案 0 :(得分:7)

如果您将评论添加为PHP,人们将无法在浏览器中看到它。

<div> 
   <!-- This HTML comment can be seen by people -->
   <?php //But this PHP comment can only be seen by me :) ?>
</div>

http://en.wikipedia.org/wiki/PHP

答案 1 :(得分:0)

我明白你的意思了。你可以用输出缓冲来做到这一点:

<?php
   // this is not
?><!-- this is sent to browser -->

并使用输出缓冲。

<?php

   ob_start();

   ?><!-- this is NOT sent to browser --> <b>This isn't sent as well!</b> <?php

   ob_end_clean();
?>

但是,如果您只想删除 注释,则需要进行一些解析:

<?php

   ob_start();

   ?><!-- this is NOT sent to browser --><?php

   $html=ob_get_clean();

   // TODO: Use a DOM parser to parse $html and remove comments from it.

?>

虽然听起来有点过度设计......

答案 2 :(得分:0)

只需交换上面的行并使用php评论:

<?php...
// Prevented this comment from being viewed