0

これは私の問題です: ajax 経由で「id」と「a html string」という 2 つの値を送信する html ドキュメントがあります。

<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 ファイルにクラスが本当に必要です。

4

2 に答える 2

1

.htaccessあなたの問題は魔法の引用符です。ファイルを使用して次のディレクティブ init を配置してオフにします。

php_flag magic_quotes_gpc Off

また、ファイルを保存するには、次を使用して1行で実行できます

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