• <tfoot id='RL2hx'></tfoot>
      <bdo id='RL2hx'></bdo><ul id='RL2hx'></ul>

    1. <small id='RL2hx'></small><noframes id='RL2hx'>

      <legend id='RL2hx'><style id='RL2hx'><dir id='RL2hx'><q id='RL2hx'></q></dir></style></legend>

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

        PHP:在不知道原始字符集的情况下将任何字符串转

        时间:2023-10-03
        <legend id='ybcbi'><style id='ybcbi'><dir id='ybcbi'><q id='ybcbi'></q></dir></style></legend>

            <tfoot id='ybcbi'></tfoot>
              <tbody id='ybcbi'></tbody>

              <bdo id='ybcbi'></bdo><ul id='ybcbi'></ul>
                • <small id='ybcbi'></small><noframes id='ybcbi'>

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

                  本文介绍了PHP:在不知道原始字符集的情况下将任何字符串转换为 UTF-8,或者至少尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个与来自世界各地的客户打交道的应用程序,当然,我希望进入我的数据库的所有内容都采用 UTF-8 编码.

                  I have an application that deals with clients from all over the world, and, naturally, I want everything going into my databases to be UTF-8 encoded.

                  对我来说的主要问题是我不知道任何字符串的来源将是什么编码 - 它可能来自文本框(使用 <form accept-charset="utf-8"> 只有在用户实际提交表单时才有用),或者它可能来自上传的文本文件,所以我真的无法控制输入.

                  The main problem for me is that I don't know what encoding the source of any string is going to be - it could be from a text box (using <form accept-charset="utf-8"> is only useful if the user is actually submitted the form), or it could be from an uploaded text file, so I really have no control over the input.

                  我需要的是一个函数或类,以确保进入我的数据库的内容尽可能采用 UTF-8 编码.我试过 iconv(mb_detect_encoding($text), "UTF-8", $text);但这有问题(如果输入是未婚夫",则返回未婚夫").我已经尝试了很多东西 =/

                  What I need is a function or class that makes sure the stuff going into my database is, as far as is possible, UTF-8 encoded. I've tried iconv(mb_detect_encoding($text), "UTF-8", $text); but that has problems (if the input is 'fiancée' it returns 'fianc'). I've tried a lot of things =/

                  对于文件上传,我喜欢要求最终用户指定他们使用的编码,并向他们展示输出的预览,但这无助于抵御讨厌的黑客(事实上,它可以让他们的生活更轻松).

                  For file uploads, I like the idea of asking the end user to specify the encoding they use, and show them previews of what the output will look like, but this doesn't help against nasty hackers (in fact, it could make their life a little easier).

                  我已经阅读了有关该主题的其他 SO 问题,但它们似乎都有细微的差异,例如我需要解析 RSS 提要"或我从网站上抓取数据"(或者,实际上,您不能").

                  I've read the other SO questions on the subject, but they seem to all have subtle differences like "I need to parse RSS feeds" or "I scrape data from websites" (or, indeed, "You can't").

                  但必须有一些东西至少有一个很好的尝试

                  But there must be something that at least has a good try!

                  推荐答案

                  您的要求非常困难.如果可能,最好让用户指定编码.以这种方式防止攻击不应该更容易或更难.

                  What you're asking for is extremely hard. If possible, getting the user to specify the encoding is the best. Preventing an attack shouldn't be much easier or harder that way.

                  但是,您可以尝试这样做:

                  However, you could try doing this:

                  iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);
                  

                  将其设置为严格可能会帮助您获得更好的结果.

                  Setting it to strict might help you get a better result.

                  这篇关于PHP:在不知道原始字符集的情况下将任何字符串转换为 UTF-8,或者至少尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 UTF-8 字符串在 PHP 中使用文件系统函数 下一篇:PHP:用 UTF-8 字符串中最接近的 7 位 ASCII 等价物替

                  相关文章

                  最新文章

                  1. <small id='kPyH4'></small><noframes id='kPyH4'>

                      <legend id='kPyH4'><style id='kPyH4'><dir id='kPyH4'><q id='kPyH4'></q></dir></style></legend>

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

                        <bdo id='kPyH4'></bdo><ul id='kPyH4'></ul>