Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 938
Alk Avatar asked Jun 16 '26 05:06

Alk


1 Answers

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)

like image 143
Andrej Ludinovskov Avatar answered Jun 18 '26 21:06

Andrej Ludinovskov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!