PHP 表格 : HTML not interpreted correctly

标签 php html forms generator

我有一个 PHP 表单,它从用户那里收集 CSS 和 HTML 代码。然后表单加载一个包含 CSS 和 HTML 的 PHP 页面。

我的问题是 HTML 显示为纯文本而不是 HTML。

在句柄 PHP 文件中,我使用 file_put_contents($newshtml, $html);

在最终的 PHP 文件中,我在 body 标签之间使用了 include "html.html";

为什么 HTML 输入没有被解释为 HTML?

处理程序 PHP 代码

<?php
    $newshtml="asset/html.html";
    $newscss="asset/style.css";
    $html=htmlentities($_POST['html']);
    $css=htmlentities($_POST['css']);
    $html=stripslashes(nl2br($html));
    $css=stripslashes(nl2br($css));

    if(!is_file($newshtml, $newscss))
    {
        $verifhtml=@fopen($newshtml, "w+");
        $verifcss=@fopen($newscss, "w+");
    }
    $verifhtml=@fopen($newshtml, "r+");
    $verifcss=@fopen($newscss, "r+");

    file_put_contents($newshtml, $html);
    file_put_contents($newscss, $css);

    header('Location: layout.php');
?>

最终的 PHP 代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" type="text/css" href="style.css" media="screen">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="initial-scale=1.0">
    <meta name="format-detection" content="telephone=no">
    <title>Important: Responsive Email Templates</title>
</head>
<body style="font-size:12px;" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?php include "html.html"; ?>
</body>
</html>

最佳答案

不要在用户输入时调用 htmlentities()。这会将所有 HTML 特殊字符转换为实体,以便它们按字面显示而不是被解释。例如,如果用户输入:

<h3>header</h3>

它将被转换为:

&lt;h3&gt;header&lt;/h3&gt;

关于PHP 表格 : HTML not interpreted correctly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20945750/

相关文章:

php - 防止 IP 欺骗

php - WAMPServer 安装 - phpMyAdmin 登录时出错 - 错误 #1193

html - 如何在 Bootstrap 中自动调整行高?

javascript - PHP 验证后提交表单后显示感谢消息

php - CURL 不能被带有自定义信号处理程序的 PHP SIGINT 杀死

php - 1000 个并发用户的服务器设置

javascript - 在 HTML/JS 中创建排行榜

html - 为什么散焦时底部会出现一些超标?

javascript - 从表单输入创建对象以传递给 Controller ​​方法

ajax - 如何在 CakePHP 中使用 Js->submit()?