mankee mankee - 3 months ago 23
JSON Question

PHP UTF-8 encoding echoes question marks

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.

Answer

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

Comments