Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I save utf-8 encoded json with PHP to a .txt file
    primarykey
    data
    text
    <p>I am trying to save some data on the server but finding some encoding problems.</p> <p>This is how I am sending my objects to PHP (which works fine but I don't think the contentType is actually doing anything):</p> <pre><code>$.post( 'myfile.php', { contentType: "application/x-www-form-urlencoded;charset=utf-8", data : myData }, function (data, textStatus, jqXHR){ //some code here } ); </code></pre> <p>and then in PHP (myfile.php):</p> <pre><code>&lt;?php header('Content-Type: text/html; charset=utf-8'); $file = 'data/theData.txt'; //the file to edit $current = file_get_contents($file); // Open the file to get existing content $a2 = json_decode( $current, true ); $data = $_POST['data']; //the data from the webpage $res = array_merge_recursive( $data, $a2 ); $resJson = json_encode( $res ); // Write the contents back to the file file_put_contents($file, $resJson); ?&gt; </code></pre> <p>As you can see I am getting the original contents of the file and decoding the json. I am then merging the result with the data sent from the webpage before re-encoding in json and put the contents back. </p> <p>This all works as expected. However, at one point in my Jquery the data sent contains a wide variety of symbols like "/ ö é ł Ż ę"</p> <p>When the file is saved each '/' has an escape character '\' in front of it and likewise the 'é' for example is '\u00e9' </p> <p>How do I override this? Should I try to convert it properly in the PHP or is their a way in the JQuery to convert back to the correct format after I have $.get('data/theData.txt'?</p> <p>Any light shed on this issue is greatly appreciated! Please excuse the poor variable names.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload