php - 保存格式化的.html文件

时间:2013-06-20 09:18:12

标签: php html file format save

这是我的问题: 我有我的html文档,我通过ajax发送两个值: “id”和“html string”

<div class="myClass"><h1 class="myClass">this is my html string </h1></div>

我在.php文件中收到这些数据,我将数据保存在.html文件中:

ajax代码:

$(".button").on("click", function(e){
            e.preventDefault();
            var string = $(".htmlString").html();
            $.ajax({
                url: "data.php",
                type: "post",
                data: { 
                    ID: "correctID",
                    htmlString: string
                },
                success: function(){
                    alert('ok');
                },
                error:function(){
                    alert('error');
                }   
            }); 
        });

php代码:

if ($_POST['id'] == "correctID") {

    $fileLocation = $_SERVER['DOCUMENT_ROOT']  . "/www/file.html";
    $file = fopen($fileLocation,"w");
    $content = $_POST['htmlString'];
    fwrite($file,$content);
    fclose($file);
}

输出.html文件内容如下:

<div class=\"myClass\"><h1 class=\"myClass\">

你看到的问题是引号前面的“\”:

如何以正确的html格式保存文件?

<div class="myClass"><h1 class="myClass">

非常感谢,看着我找到了DOMDocument :: saveHTML,但是我无法使用它,我是非常新的PHP ..,我真的需要我的html文件中的类。

2 个答案:

答案 0 :(得分:0)

你的问题是神奇的引号,请使用.htaccess文件并将以下指令设置为init来关闭它。

php_flag magic_quotes_gpc Off

另外,要保存文件,您可以使用

进行一行
$file_content;
file_put_contents("filename.html", $file_content);

答案 1 :(得分:0)

您的问题是由magic_quotes_gpc引起的。

2种可能的解决方案:

  • 禁用magic_quotes_gpc
  • 在`$ _POST ['htmlString']
  • 上调用stripslashes