Home  >  Q&A  >  body text

php如何显示curl返回的Unicode escape strings?如"\u6b22\u8fce\u56de\u6765"

php的curl返回string(42) "{"ret":1,"msg":"u6b22u8fceu56deu6765"}",想知道该怎样显示正确的文字? "u6b22u8fceu56deu6765"对应的是"欢迎回来"

淡淡烟草味淡淡烟草味2686 days ago1518

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-05-16 13:07:33

    If you can handle the server side, you can control whether Chinese is not encoded through the second parameter of json_encode.
    http://php.net/manual/zh/func...
    If you can’t process the server, you can process the result returned by curl. First decode json, and then re-json_encode (also controlled according to the second parameter)

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 13:07:33

    The answerer still didn't explain it in detail enough, so the newbie can only search slowly on the Internet...I found a perfect solution:

    <?php
    $arr = array('p1'=>'nihao','p2'=>2,'ch'=>'码农你好!');
    $json = json_encode($arr);
    echo decodeUnicode($json);
    
    function decodeUnicode($str){
        return preg_replace_callback('/\\u([0-9a-f]{4})/i',
            create_function(
                '$matches',
                'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
            ),
            $str);
    }
    ?>

    Reference: http://www.manongjc.com/artic...

    reply
    0
  • Cancelreply