3

I'm trying to echo a json_response with unicode characters using the following code:

function utf8ize($d) {
if (is_array($d)) {
    foreach ($d as $k => $v) {
        $d[$k] = utf8ize($v);
    }
} else if (is_string ($d)) {
    return utf8_encode($d);
}
return $d;
}

used like this:

echo json_encode(utf8ize($response));

The problem with this is that some characters are encoded properly and other characters like ć and ś are sent as question marks as seen in the below image:

enter image description here

I'm not sure how to fix this.

Alk
  • 5,215
  • 8
  • 47
  • 116

2 Answers2

2

According to http://php.net/manual/en/function.utf8-encode.php utf8_encode encodes data from ISO-8859-1 to UTF-8.

However, Polish lang has iso-8859-2 charset so you should use

iconv('iso-8859-2', 'utf-8', $d) instead of utf8_encode($d)

Andrej
  • 7,474
  • 1
  • 19
  • 21
1

You could try to execute

SET NAMES utf8

As a query. After you've made the connection.

Then just in case set this header

header('Content-type: text/plain; charset=utf-8');

Then try again

Robin Knaapen
  • 606
  • 4
  • 12
  • `SET NAMES utf8` changed the output to this : `"Warto\u0139\u009b\u00c4\u0087 wyra\u0139\u017aenia 5 - 2 \u00c2\u02c7 4 jest r\u0102\u0142wna` That's still not displaying properly for me – Alk Sep 03 '16 at 23:36
  • Nevermind, after I deleted the utf8_encode it works fine. Thanks – Alk Sep 03 '16 at 23:37
  • Not sure if you have any html, but the use the header like `header('Content-type: text/html; charset=utf-8');` in that case – Robin Knaapen Sep 03 '16 at 23:43
  • Nope, this is a PHP script called by a mobile app in Java, no HTML going on – Alk Sep 03 '16 at 23:46
  • 1
    Okay, but wanted to be sure. Haha – Robin Knaapen Sep 03 '16 at 23:46