You can do:
$json = json_encode($response);
header('Content-type: application/json; charset=ISO-8859-9');
echo mb_convert_encoding($json, "ISO-8859-9", "UTF-8");
Assuming that strings in $response
is in utf-8. But I would strongly suggest that you just use utf-8 all the way through.
Edit: Sorry, just realised that won't work, since json_encode
escapes unicode points as javascript escape codes. You'll have to convert these to utf-8 sequences first. I don't think there are any built-in functionality for that, but you can use a slightly modified variation of this library to get there. Try the following:
function unicode_hex_to_utf8($hexcode) {
$arr = array(hexdec(substr($hexcode[1], 0, 2)), hexdec(substr($hexcode[1], 2, 2)));
$dest = '';
foreach ($arr as $src) {
if ($src < 0) {
return false;
} elseif ( $src <= 0x007f) {
$dest .= chr($src);
} elseif ($src <= 0x07ff) {
$dest .= chr(0xc0 | ($src >> 6));
$dest .= chr(0x80 | ($src & 0x003f));
} elseif ($src == 0xFEFF) {
// nop -- zap the BOM
} elseif ($src >= 0xD800 && $src <= 0xDFFF) {
// found a surrogate
return false;
} elseif ($src <= 0xffff) {
$dest .= chr(0xe0 | ($src >> 12));
$dest .= chr(0x80 | (($src >> 6) & 0x003f));
$dest .= chr(0x80 | ($src & 0x003f));
} elseif ($src <= 0x10ffff) {
$dest .= chr(0xf0 | ($src >> 18));
$dest .= chr(0x80 | (($src >> 12) & 0x3f));
$dest .= chr(0x80 | (($src >> 6) & 0x3f));
$dest .= chr(0x80 | ($src & 0x3f));
} else {
// out of range
return false;
}
}
return $dest;
}
print mb_convert_encoding(
preg_replace_callback(
"~\\\\u([1234567890abcdef]{4})~", 'unicode_hex_to_utf8',
json_encode($response)),
"ISO-8859-9", "UTF-8");