php json_encode 将日语字符转换为 UTF-8 [英] Convert Japanese characters to UTF-8 for php json_encode
问题描述
我的(UTF-8 编码)数据库利用规约"中有一个日语字符串,它被传递给 php 的 json_encode 函数并收到此警告:
I have a Japanese string in my (UTF-8 encoded) database '利用規約' which is passed to php's json_encode function and receive this warning:
PHP 警告:json_encode():参数中的 UTF-8 序列无效
PHP Warning: json_encode(): Invalid UTF-8 sequence in argument
有没有办法将其转换为有效的 UTF-8 以避免这种情况?如果db是utf-8,为什么php的字符串会有问题?
Is there a way to convert this to valid UTF-8 in order to avoid this? If the db is utf-8 why would php have a problem with the string?
更新:
事实证明该错误实际上是由于几行前 sprintf 和多字节字符的问题造成的.
Turns out the error was in fact due to a problem with sprintf and multibyte characters a couple lines earlier.
推荐答案
utf8_encode 应该适合你.
utf8_encode should work for you.
http://www.php.net/manual/en/function.utf8-encode.php
如果 utf8_encode 不起作用,请尝试 mb_convert_encoding:
If utf8_encode doesn't work, try mb_convert_encoding:
mb_convert_encoding($string,"UTF-8","auto");
这篇关于php json_encode 将日语字符转换为 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!