• <tfoot id='w2t87'></tfoot>
  • <i id='w2t87'><tr id='w2t87'><dt id='w2t87'><q id='w2t87'><span id='w2t87'><b id='w2t87'><form id='w2t87'><ins id='w2t87'></ins><ul id='w2t87'></ul><sub id='w2t87'></sub></form><legend id='w2t87'></legend><bdo id='w2t87'><pre id='w2t87'><center id='w2t87'></center></pre></bdo></b><th id='w2t87'></th></span></q></dt></tr></i><div id='w2t87'><tfoot id='w2t87'></tfoot><dl id='w2t87'><fieldset id='w2t87'></fieldset></dl></div>

    <small id='w2t87'></small><noframes id='w2t87'>

      <legend id='w2t87'><style id='w2t87'><dir id='w2t87'><q id='w2t87'></q></dir></style></legend>
          <bdo id='w2t87'></bdo><ul id='w2t87'></ul>
      1. MongoDB PHP UTF-8 问题

        时间:2023-10-03

        • <small id='jPw1h'></small><noframes id='jPw1h'>

            <tbody id='jPw1h'></tbody>
          • <bdo id='jPw1h'></bdo><ul id='jPw1h'></ul>
            <tfoot id='jPw1h'></tfoot>
            <legend id='jPw1h'><style id='jPw1h'><dir id='jPw1h'><q id='jPw1h'></q></dir></style></legend>
            <i id='jPw1h'><tr id='jPw1h'><dt id='jPw1h'><q id='jPw1h'><span id='jPw1h'><b id='jPw1h'><form id='jPw1h'><ins id='jPw1h'></ins><ul id='jPw1h'></ul><sub id='jPw1h'></sub></form><legend id='jPw1h'></legend><bdo id='jPw1h'><pre id='jPw1h'><center id='jPw1h'></center></pre></bdo></b><th id='jPw1h'></th></span></q></dt></tr></i><div id='jPw1h'><tfoot id='jPw1h'></tfoot><dl id='jPw1h'><fieldset id='jPw1h'></fieldset></dl></div>

                  本文介绍了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 West Europe (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:

                  • mb_convert_encoding()
                  • utf8_encode(utf8_decode($string))

                  您应该始终确保您的字符串是 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 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:UTF-8 的多字节安全 wordwrap() 函数 下一篇:从 php7 中删除了 utf8_(en|de) 代码?

                  相关文章

                  最新文章

                  <small id='xO2Dj'></small><noframes id='xO2Dj'>

                  1. <tfoot id='xO2Dj'></tfoot>

                      <i id='xO2Dj'><tr id='xO2Dj'><dt id='xO2Dj'><q id='xO2Dj'><span id='xO2Dj'><b id='xO2Dj'><form id='xO2Dj'><ins id='xO2Dj'></ins><ul id='xO2Dj'></ul><sub id='xO2Dj'></sub></form><legend id='xO2Dj'></legend><bdo id='xO2Dj'><pre id='xO2Dj'><center id='xO2Dj'></center></pre></bdo></b><th id='xO2Dj'></th></span></q></dt></tr></i><div id='xO2Dj'><tfoot id='xO2Dj'></tfoot><dl id='xO2Dj'><fieldset id='xO2Dj'></fieldset></dl></div>

                    1. <legend id='xO2Dj'><style id='xO2Dj'><dir id='xO2Dj'><q id='xO2Dj'></q></dir></style></legend>
                      • <bdo id='xO2Dj'></bdo><ul id='xO2Dj'></ul>