0

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

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

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

阿贾克斯代码:

$(".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 文件中的类。

4

2 回答 2

1

.htaccess您的问题是魔术引号,通过使用文件并放置以下指令 init 将其关闭。

php_flag magic_quotes_gpc Off

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

$file_content;
file_put_contents("filename.html", $file_content);
于 2013-06-20T09:21:44.867 回答
0

您的问题是由 magic_quotes_gpc 引起的。

2个可能的解决方案:

  • 禁用magic_quotes_gpc
  • 在 `$_POST['htmlString'] 上调用 stripslashes
于 2013-06-20T09:22:29.593 回答