MongoDB PHP UTF-8问题 [英] MongoDB PHP UTF-8 problems

查看:65
本文介绍了MongoDB PHP UTF-8问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我需要插入以下文档:

Assume that I need to insert the following document:

{
    title: 'Péter'
}

(请注意é)

当我使用以下PHP代码时,它给我一个错误...:

It gives me an error when I use the following PHP-code ... :

$db->collection->insert(array("title" => "Péter"));

...,因为它必须是utf-8.

... because it needs to be utf-8.

所以我应该使用以下代码行:

So I should use this line of code:

$db->collection->insert(array("title" => utf8_encode("Péter")));

现在,当我请求文档时,我仍然必须对其进行解码...:

Now, when I request the document, I still have to decode it ... :

$document = $db->collection->findOne(array("_id" => new MongoId("__someID__")));
$title = utf8_decode($document['title']);

是否有某种方法可以自动执行此过程?我可以更改MongoDB的字符编码(我正在迁移使用cp1252西欧(latin1)的MySQL数据库吗?

Is there some way to automate this process? Can I change the character-encoding of MongoDB (I'm migrating a MySQL-database that's using cp1252 West Europe (latin1)?

我已经考虑过更改Content-Type-header,问题是所有静态字符串(硬编码)都不是utf8 ...

I already considered changing the Content-Type-header, problem is that all static strings (hardcoded) aren't utf8...

提前谢谢! 蒂姆

推荐答案

JSON和BSON只能编码/解码有效的UTF-8字符串,如果您的数据(包含的输入)不是UTF-8,则需要在传递之前进行转换到任何与JSON相关的系统中,如下所示:

JSON and BSON can only encode / decode valid UTF-8 strings, if your data (included input) is not UTF-8 you need to convert it before passing it to any JSON dependent system, like this:

$string = iconv('UTF-8', 'UTF-8//IGNORE', $string); // or
$string = iconv('UTF-8', 'UTF-8//TRANSLIT', $string); // or even
$string = iconv('UTF-8', 'UTF-8//TRANSLIT//IGNORE', $string); // not sure how this behaves

我个人比较喜欢第一种选择,请参见 iconv() 手册页.其他替代方法包括:

Personally I prefer the first option, see the iconv() manual page. Other alternatives include:

您应该始终确保您的字符串是UTF-8编码的,甚至包括用户提交的字符串,但是由于您提到要从MySQL迁移到MongoDB,因此您是否尝试过将当前数据库导出为CSV并使用导入Mongo随附的脚本?他们应该处理这个...

You should always make sure your strings are UTF-8 encoded, even the user-submitted ones, however since you mentioned that you're migrating from MySQL to MongoDB, have you tried exporting your current database to CSV and using the import scripts that come with Mongo? They should handle this...

我提到BSON只能处理UTF-8,但是我不确定这是否是真的,我有一个模糊的想法,即BSON使用UTF-16或UTF-32编码/解码数据,但现在无法检查.

I mentioned that BSON can only handle UTF-8, but I'm not sure if this is exactly true, I have a vague idea that BSON uses UTF-16 or UTF-32 to encode / decode data, but I can't check now.

这篇关于MongoDB PHP UTF-8问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆