php 如何使用法语口音对数组进行 json_encode?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6928982/
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
How to json_encode array with french accents?
提问by Natkeeran
I have an array item with a French accent ([WIPDescription] => Recette Soupe à lOignon Sans Boeuf US). The data is being properly pulled from the database (mysql).
我有一个带有法国口音的数组项([WIPDescription] => Recette Soupe à lOignon Sans Boeuf US)。正在从数据库 (mysql) 中正确提取数据。
However, when I try to encode this as json using the php built in json_encode it produces a null json value (OS X server: php 5.3.4, json 1.2.1 enabled).
但是,当我尝试使用 json_encode 中内置的 php 将其编码为 json 时,它会生成一个 null json 值(OS X 服务器:php 5.3.4,启用 json 1.2.1)。
In a Linux server, the description is cut off after the first accent character.
在 Linux 服务器中,描述在第一个重音字符之后被截断。
I tried all the json_encode options with no success. Any suggestions?
我尝试了所有 json_encode 选项但没有成功。有什么建议?
Thank you.
谢谢你。
采纳答案by Wrikken
json_encode
only wants utf-8
. Depending on your character set, you can use iconv
or utf8_encode
beforecalling json_encode
on your variable. Probably with array_walk_recursive
.
json_encode
只想要utf-8
。根据您的字符集,您可以在调用变量之前使用iconv
或。大概与.utf8_encode
json_encode
array_walk_recursive
As requested, an unfinishedway to alter an array, with the assumptions that (1) it doesn't contain objects, and (2) the array keys are in ascii / lower bounds, so can be left as is:
根据要求,一种未完成的更改数组的方法,假设(1)它不包含对象,并且(2)数组键在 ascii /下界,因此可以保持原样:
$current_charset = 'ISO-8859-15';//or what it is now
array_walk_recursive($array,function(&$value) use ($current_charset){
$value = iconv('UTF-8//TRANSLIT',$current_charset,$value);
});
回答by Pete
I found this to be the easiest way to deal with it
我发现这是处理它的最简单方法
echo json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
JSON_PRETTY_PRINT - makes is readable
JSON_UNESCAPED_UNICODE - encodes characters correctly
JSON_UNESCAPED_SLASHES - gets rid of escape slash '\'
also notice that these option are separated by a pipe '|'
JSON_PRETTY_PRINT - 使可读
JSON_UNESCAPED_UNICODE - 正确编码字符
JSON_UNESCAPED_SLASHES - 去掉转义斜杠 '\'
还要注意这些选项由管道 '|' 分隔
回答by sdespont
Another solution would be to use htmlentities
or utf8_encode
before using json_encode
to pass the encoded char
另一种解决方案是使用htmlentities
或utf8_encode
之前使用json_encode
来传递编码的字符
like this:
像这样:
$array = array('myvalue' => utf8_encode('ééàà'));
return json_encode($array);
Or using htmlentities
:
或使用htmlentities
:
$array = array('myvalue' => htmlentities('ééàà'));
return json_encode($array);
回答by Mike Bryant
<?
$sql=mysql_query("SELECT * FROM TABLE...");
while($row=mysql_fetch_array($sql))
{
$output[]=array_map("utf8_encode", $row);
}
print(json_encode($output));
mysql_close();
?>
回答by AlienWebguy
回答by Ali Mhamad Slim
$json = utf8_encode($string);
$json = json_decode($json);
回答by mihaidp
If you deal with diacritics you can also add JSON_PARTIAL_OUTPUT_ON_ERROR and it will remove only the problems and leave the rest intact. I used utf_encode before I found this but it was messing up diacritics.
如果您处理变音符号,您还可以添加 JSON_PARTIAL_OUTPUT_ON_ERROR,它将仅删除问题,其余部分保持不变。我在发现这个之前使用了 utf_encode ,但它弄乱了变音符号。
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR);