2

I am trying to save some data on the server but finding some encoding problems.

This is how I am sending my objects to PHP (which works fine but I don't think the contentType is actually doing anything):

$.post(
  'myfile.php',
   {
       contentType: "application/x-www-form-urlencoded;charset=utf-8",
       data : myData
   },
   function (data, textStatus, jqXHR){
      //some code here
   }
);

and then in PHP (myfile.php):

<?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);
?>

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.

This all works as expected. However, at one point in my Jquery the data sent contains a wide variety of symbols like "/ ö é ł Ż ę"

When the file is saved each '/' has an escape character '\' in front of it and likewise the 'é' for example is '\u00e9'

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'?

Any light shed on this issue is greatly appreciated! Please excuse the poor variable names.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Patrick
  • 6,495
  • 6
  • 51
  • 78
  • possible duplicate of [Convert a JSON into a UTF-8 string](http://stackoverflow.com/questions/4409039/convert-a-json-into-a-utf-8-string) – Ignacio Vazquez-Abrams Apr 28 '12 at 13:48
  • @IgnacioVazquez-Abrams: I don't think that's a dup. – Alix Axel Apr 28 '12 at 14:02
  • use base64_encode() on the $_POST value and give it to us. This preserves the string binary safe for us, so we can tell you if its utf8 or not. – goat Apr 28 '12 at 14:39
  • @Alix: Asker wants their JSON encoded as UTF-8, not ASCII with Unicode escapes. PHP doesn't do that. – Ignacio Vazquez-Abrams Apr 28 '12 at 15:22
  • @chris Sorry, my PHP is very rusty. The $_POST['data'] value is an array so how should I base64 encode it? – Patrick Apr 28 '12 at 16:23
  • `echo base64_encode($_POST['data']);` and give it to us. – goat Apr 28 '12 at 16:24
  • I couldn't work out how to get echo to work because as soon as the script has run, it goes to a new page (I am using JQuery Mobile). I tried this but think its wrong. All I had was an empty text file $data = base64_encode($_POST['data']); file_put_contents($file, $data); – Patrick Apr 28 '12 at 16:50
  • Hello Patrick, Chris asked you to do give him the output of the code he mentioned in his comment, not to save it into a file. – KBN Apr 28 '12 at 17:07
  • Hi @xFortyFourx I was having problems using echo in the php script. It just runs the script and after there is a JQuery line to go to a new page. So I tried to write the output to a file, but it was just blank. Am I doing something stupid!? I took away the '$.mobile.changePage( "#page_thisPage", { transition: "slidedown"} );' jquery mobile line, but still nothing was echoed. – Patrick Apr 28 '12 at 17:27
  • Is the original webpage utf-8? – ilanco Apr 28 '12 at 17:29
  • Hi Patrick, I hope this helps http://stackoverflow.com/questions/4839402/how-to-write-file-in-utf-8-format – KBN Apr 28 '12 at 17:33

2 Answers2

1

The link provided by @chalet16 helped but if JSON_UNESCAPED_UNICODE doesn't work for you this does the job!

$myString = $json_encode($myObject); 
//After initially encoding to json there are escapes on all '/' and characters like ö é ł Ż ę

//First unescape slashes:
$myString = str_replace("\/","/",$myString);

//Then escape double quotes
$myString = str_replace('"','\\"',$myString);

//And Finally:
$myNewString = json_decode('"'.$myString.'"');
Patrick
  • 6,495
  • 6
  • 51
  • 78
0

If you are using PHP >=5.4.0, you can use JSON_UNESCAPED_UNICODE option in json_encode function. Please look at php.net: json_encode for more details.

For PHP < 5.4.0, there are some comments about how to do this in user contributed notes on the same page, but it is uncertain that it will work correctly or not.

chalet16
  • 197
  • 2
  • Thanks @chalet16 That helped greatly. Sadly the JSON_UNESCAPED_UNICODE option didn't work for me but the other comments fixed it. – Patrick Apr 28 '12 at 19:33