如何阻止 HTML 代码中的任何 PHP 代码?我的应用程序中有一个函数,用户可以在其中创建一个 HTML 页面,其中包含 HTML 代码、Javascript 代码和 CSS 代码。但是我不能让他保存任何 PHP 代码,因为这些内容会保存在一个文件中并加载到服务器中。<html> <head> </head> <body> <h1>This is allowed</h1> <?php echo "DELETE THIS LINE"; ?> </body> <script> console.log("THIS IS ALLOWED"); </script></html>我需要注释或删除 PHP 代码,但我不知道该怎么做。
2 回答
Smart猫小萌
TA贡献1911条经验 获得超7个赞
不要包含该文件,PHP 将不会执行(它将在 HTML 源代码中作为未知的 HTML 标记但不会执行)。只需阅读并输出:
$output = file_get_contents("/path/to/file.html");
echo $output;
//or
readfile("/path/to/file.html");
或者,如果您想包含它,请在写入文件之前对 PHP 进行注释(它将是 HTML 源代码中的注释):
$output = str_replace(['<?php', '?>'], ['<!--', '?>'], $html);
file_put_contents("/path/to/file.html", $output);
//where you want to use it
include("/path/to/file.html");
- 2 回答
- 0 关注
- 293 浏览
添加回答
举报
0/150
提交
取消