json with special characters like é

主宰稳场 提交于 2019-11-28 10:50:11
YvonBlais

Just like the first anwser

Do you use a database? If Yes, make sure the database table is declared UFT8 How is declared the HTML page? UTF-8 IS the string in the PHP script file? If yes, make sure the file has a UTF-8 file format

You could also use utf8_encode (to send to HTML) and utf8_decode (to receive) but not the right way

If I write them like (é) and don't pre-encode them the full value in this array entry is not shown.

What should I do here?

In JSON you do not HTML-encode values. You send them literally (é) and set set Content-Type correctly:

header('Content-Type: application/json; Charset=UTF-8');

Declare the encoding your data is in, of course.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!