0

I know there are a lot of topics out there concerning this problem, but I've spend the last hours trying various approaches an I'm nowhere close to the solution. So here it goes ...

This is my jquery:

$.post('inc/app_json_f.php',{params:priv_params}, function (data) {
  console.log(data);
  // displays: \u00e2\u0082\u00ac instead of €
  data = JSON.parse(data);
  console.log(data);
  // displays: â¬â¬ instead of €
}

This is my php (the app_json_f.php page):

$qry = 'select ... from ... where ...';
$data = $db->do_select($qry);

echo json_encode(f_utf8_json($data));

The f_utf8_json function will check the $data and convert every value with utf8_encode().

At this moment, the echo json_encode(...) will display the following in the console:

\u00e2\u0082\u00ac

Whereas I would like to see the € sign.

The data comes from a MySQL database which has collation utf8_general_ci. PHP's charset is UTF-8.

Any suggestions?

  • 1
    PHP defaults to connect with latin1 charset maybe thats the problem. So you need to change the connection charset of your PHP MySQL connection -> see http://stackoverflow.com/questions/10829816/set-character-set-using-mysqli for mysqli and see http://stackoverflow.com/questions/4361459/php-pdo-charset-set-names for PDO – Raymond Nijland Feb 28 '17 at 13:33
  • Forgot to mention that: I already added mysqli_set_charset($this->db_conn, "utf8"); right after connecting. – nickvandyck Feb 28 '17 at 13:37

3 Answers3

0

for send file using curl you need to use this working code.

if(!empty($postfields['image'])){
$file_name_with_full_path = $postfields['image'];
  if (function_exists('curl_file_create')) { // php 5.6+
    $cFile = curl_file_create($file_name_with_full_path);
  } else { // 
    $cFile = '@' . realpath($file_name_with_full_path);
  }
  $postfields['fileToUpload'] =  $cFile;

}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$result=curl_exec ($ch);
curl_close ($ch);

User curl_file_create

0

When using json_encode(), include JSON_UNESCAPED_UNICODE as the second argument so that you don't get \u0123 type codes, but instead get UTF-8.

The hex for UTF-8 encoding of is E2ACE2AC. (Ditto for MySQL's utf8 and utf8mb4.)

If you get €, then you have "Mojibake".

If you take UNHEX(E2ACE2AC) and treat it as any of cp1250, cp1256, cp1257, latin1, latin5, latin7, you get â¬â¬. It sounds like you are compounding errors to get there.

See the "best practices" and other debugging tips here: Trouble with utf8 characters; what I see is not what I stored

Community
  • 1
  • 1
Rick James
  • 135,179
  • 13
  • 127
  • 222
0

By removing the f_utf8_json function (the one that converts the values to utf8 with utf8_encode()), everything works ok. Strange, because I added this function earlier in the project because it was necessary to get the right results. I have to double check, but possibly another database setting was the cause of this.

Problem solved.