php json_encode 中德语变音的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13602075/
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
problems with german umlauts in php json_encode
提问by Valentino Ru
I'm getting troubles with data from a database containing german umlauts. Basically, whenever I receive a data containing umlauts, it is a black square with an interrogation mark. I solved this by putting
我在处理包含德国变音符号的数据库中的数据时遇到问题。基本上,每当我收到包含变音的数据时,它都是一个带有询问标记的黑色方块。我解决了这个问题
mysql_query ('SET NAMES utf8')
before the query.
在查询之前。
The problem is, as soon as I use json_encode(...)on a result of a query, the value containing an umlaut gets null. I can see this by calling the php-file directly in the browser. Are there other solution than replacing this characters before encoding to JSON and decoding it in JS?
问题是,只要我json_encode(...)在查询结果上使用,包含变音符号的值就会得到null. 我可以通过直接在浏览器中调用 php 文件来看到这一点。除了在编码为 JSON 并在 JS 中解码之前替换这些字符之外,还有其他解决方案吗?
采纳答案by Sirko
You probably just want to show the texts somehow in the browser, so one option would be to change the umlauts to HTML entities by using htmlentities().
您可能只想在浏览器中以某种方式显示文本,因此一种选择是使用htmlentities().
The following test worked for me:
以下测试对我有用:
<?php
$test = array( 'bla' => '??ü' );
$test['bla'] = htmlentities( $test['bla'] );
echo json_encode( $test );
?>
回答by GDY
Check out this pretty elegant solution mentioned here:
看看这里提到的这个非常优雅的解决方案:
json_encode($json_full, JSON_UNESCAPED_UNICODE);
If the problem isn't anywhere else in your code this should fix it.
如果问题不在您的代码中的其他任何地方,这应该可以解决它。
Edit: Umlaut problems can be caused by a variety of sources like the charset of you HTML document, the database format or some previous php functions (multibytefunctions is something you should look into). These problems tend to be the most annoying ones because there are pretty hard to track in some cases (altough this isn't is bad as it was a few years ago). The function above fixes – as asked – umlaut problems with json_encode … but it may be that the problem is a different one (or multiple).
编辑:变音问题可能由多种来源引起,例如 HTML 文档的字符集、数据库格式或某些以前的 php 函数(多字节函数是您应该研究的内容)。这些问题往往是最烦人的,因为在某些情况下很难跟踪(尽管这并不像几年前那样糟糕)。上面的函数修复了 - 正如所问的 - json_encode 的变音问题……但问题可能是不同的(或多个)。
回答by mffap
I know this might be old but here a better solution:
我知道这可能很旧,但这里有一个更好的解决方案:
Define the document type with utf-8 charset:
使用 utf-8 字符集定义文档类型:
<?php header('Content-Type: application/json; charset=utf-8'); ?>
Make sure that all content is utf_encoded. JSON works only with utf-8!
确保所有内容都是 utf_encoded。JSON 仅适用于 utf-8!
function encode_items(&$item, $key)
{
$item = utf8_encode($item);
}
array_walk_recursive($rows, 'encode_items');
Hope this helps someone.
希望这可以帮助某人。
回答by Tarsis
The only important point here is that json_encode() only supports UTF-8 encoding. http://www.php.net/manual/en/function.json-encode.php
这里唯一重要的一点是 json_encode() 只支持 UTF-8 编码。 http://www.php.net/manual/en/function.json-encode.php
All string data must be UTF-8 encoded.
所有字符串数据都必须采用 UTF-8 编码。
So when you have any special characters in a non utf-8 string json_encode will return a null Value.
因此,当您在非 utf-8 字符串中有任何特殊字符时,json_encode 将返回空值。
So either you switch the whole project to utf-8 or you make sure you utf8_encode() any string before using json_encode().
因此,要么将整个项目切换为 utf-8,要么确保在使用 json_encode() 之前使用 utf8_encode() 任何字符串。
回答by ShirtsofHolland
make sure the translation file itself was explicitely stored as UTF-8
确保翻译文件本身明确存储为 UTF-8
After that reload cache blocks and translations
之后重新加载缓存块和翻译

