Php / json:decode utf8? [英] Php/json: decode utf8?
问题描述
我存储一个json字符串,其中包含一些(中文?)字符在mysql数据库。
数据库中的示例:
I store a json string that contains some (chinese ?) characters in a mysql database. Example of what's in the database:
normal.text.\u8bf1\u60d1.rest.of.text
在我的PHP页面上我只是做一个json_decode从mysql接收, t显示正确,它显示像½±è§
On my PHP page I just do a json_decode of what I receive from mysql, but it doesn't display right, it shows things like "½±è§�"
我试图在我的开始执行SET NAMES'utf8'文件,没有改变任何东西。
我的网页上已经有以下标题:
I've tried to execute the "SET NAMES 'utf8'" query at the beginning of my file, didn't change anything. I already have the following header on my webpage:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
当然所有的php文件都是用UTF-8编码的。
And of course all my php files are encoded in UTF-8.
你有什么想法如何很好地显示这些\uXXXX字符?
Do you have any idea how to display these "\uXXXX" characters nicely?
推荐答案
Unicode不是UTF-8!
Unicode is not UTF-8!
$ echo -en '\x8b\xf1\x60\xd1\x00\n' | iconv -f unicodebig -t utf-8
诱惑
这是一个奇怪的编码你有。我猜每个字符的正常文本是一个字节长(US-ASCII)?然后你必须提取\u ....序列,转换序列在一个两个字节字符,并转换该字符与 iconv(unicodebig,utf-8,$字符)
更改为UTF-8字符(请参见 iconv < a>在PHP文档中)。这在我身边有效:
This is a strange "encoding" you have. I guess each character of the normal text is "one byte" long (US-ASCII)? Then you have to extract the \u.... sequences, convert the sequence in a "two byte" character and convert that character with iconv("unicodebig", "utf-8", $character)
to an UTF-8 character (see iconv in the PHP-documentation). This worked on my side:
$in = "normal.text.\u8bf1\u60d1.rest.of.text";
function ewchar_to_utf8($matches) {
$ewchar = $matches[1];
$binwchar = hexdec($ewchar);
$wchar = chr(($binwchar >> 8) & 0xFF) . chr(($binwchar) & 0xFF);
return iconv("unicodebig", "utf-8", $wchar);
}
function special_unicode_to_utf8($str) {
return preg_replace_callback("/\\\u([[:xdigit:]]{4})/i", "ewchar_to_utf8", $str);
}
echo special_unicode_to_utf8($in);
否则,我们需要更多关于数据库中的字符串如何编码的信息。
Otherwise we need more Information on how your string in the database is encoded.
这篇关于Php / json:decode utf8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!