2016-09-03 23:20
浏览 55

PHP UTF-8编码回应问号

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dragoninasia2014
    dragoninasia2014 2016-09-03 23:27

    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

    点赞 评论
  • duanchazhou6779
    duanchazhou6779 2016-09-03 23:28

    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)

    点赞 评论