json with special characters like é

前端 未结 3 1493
迷失自我
迷失自我 2020-12-10 01:34

I\'m developing a dependent select script using jQuery, PHP and JSON as the response.

Everything goes well except for using special characters like French ones (é ,

3条回答
  •  爱一瞬间的悲伤
    2020-12-10 02:13

    This worked for me, hopefully it will work for anyone else experiencing similar issues.

    $title = 'é';
    $title = mb_convert_encoding($title, "UTF-8", "HTML-ENTITIES");
    
    header('Content-Type: application/json; Charset="UTF-8"');
    echo json_encode(array('title' => $title));
    

    The mb_convert_encoding function takes a value and converts it from (in this case) HTML-ENTITIES to UTF-8.

    See here for me details on the function http://php.net/manual/en/function.mb-convert-encoding.php

提交回复
热议问题