php 带有特殊字符(如é)的json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7284535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:23:46  来源:igfitidea点击:

json with special characters like é

phpjqueryjsonhtmlspecialchars

提问by medk

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

我正在开发一个使用 jQuery、PHP 和 JSON 作为响应的依赖选择脚本。

Everything goes well except for using special characters like French ones (é , è , à...)

除了使用法语字符(é、è、à...)等特殊字符外,一切都进行得很顺利

if I pre-encode them like (é , è , à) (Here I'm using spaces between the ampersand and the rest of the word to prevent auto encoding in my question) it works but when rendered with jquery the characters are not converted to what they should look like (é...), instead they are shown as is (é)

如果我像 (é , è , à) 那样对它们进行预编码(这里我在 & 号和单词的其余部分之间使用空格以防止在我的问题中进行自动编码)它可以工作,但是当使用 jquery 呈现字符时没有转换成它们应该的样子 (é...),而是按原样显示 (é)

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?

我应该在这里做什么?

Thanks.

谢谢。

采纳答案by 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

你使用数据库吗?如果是,请确保已声明数据库表 UFT8 如何声明 HTML 页面?UTF-8 是 PHP 脚本文件中的字符串吗?如果是,请确保文件具有 UTF-8 文件格式

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

您也可以使用 utf8_encode(发送到 HTML)和 utf8_decode(接收)但不是正确的方式

回答by Tomalak

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:

在 JSON 中,您不对值进行 HTML 编码。您按字面意思发送它们 (é) 并正确设置 set Content-Type:

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

Declare the encoding your data is in, of course.

当然,声明您的数据所在的编码。

回答by rharvey

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.

mb_convert_encoding 函数接受一个值并将其从(在本例中)HTML-ENTITIES 转换为 UTF-8。

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

在这里为我查看有关该功能的详细信息http://php.net/manual/en/function.mb-convert-encoding.php