<small id='24vnV'></small><noframes id='24vnV'>

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

    1. <legend id='24vnV'><style id='24vnV'><dir id='24vnV'><q id='24vnV'></q></dir></style></legend>
      <tfoot id='24vnV'></tfoot>
          <bdo id='24vnV'></bdo><ul id='24vnV'></ul>

        在 UTF-8 编码的字符串上使用 str_split

        时间:2023-10-03

        <small id='7YYKT'></small><noframes id='7YYKT'>

          <tbody id='7YYKT'></tbody>

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

                  本文介绍了在 UTF-8 编码的字符串上使用 str_split的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我目前正在处理一个项目,我想我应该继续学习如何使用 PDO,而不是使用常规的 MySQL 查询.

                  I'm currently working on a project, and instead of using regular MySQL queries I thought I'd go ahead and learn how to use PDO.

                  我有一个表叫参赛者,数据库、表和所有的列都是utf-8.我在参赛者表中有十个条目,它们的名称"列包含 åäö 等字符.

                  I have a table called contestants, both the database, the table, and all of the columns are in utf-8. I have ten entries in the contestant table, and their column "name" contains characters such as åäö.

                  现在,当我从数据库中获取一个条目并使用 var_dump 名称时,我得到了一个很好的结果,一个包含所有特殊字符的字符串.但我需要做的是将字符串按字符拆分,将它们放入一个数组中,然后再进行洗牌.

                  Now, when I fetch an entry from the database, and var_dump the name, I get a good result, a string with all the special characters intact. But what I need to do is to split the string by characters, to get them in an array that I then shuffle.

                  例如,我有这个字符串:测试 ÅÄÖ Tåän

                  For instance, I have this string: Test ÅÄÖ Tåän

                  当我运行 str_split 时,我将每个字符放入数组中它自己的键中.唯一的问题是所有特殊字符都显示为: ,意味着数组将如下所示:

                  And when I run str_split I get each character in it's own key in an array. The only issue is that all the special characters display as this: �, meaning the array will be like this:

                  Array
                  (
                      [0] => T
                      [1] => e
                      [2] => s
                      [3] => t
                      [4] =>  
                      [5] => �
                      [6] => �
                      [7] => �
                      [8] => �
                      [9] => �
                      [10] => �
                      [11] =>  
                      [12] => T
                      [13] => �
                      [14] => �
                      [15] => �
                      [16] => �
                      [17] => n
                  )
                  

                  如您所见,它不仅会弄乱字符,还会在 str_split 过程中复制它们.我尝试了几种拆分字符串的方法,但它们都有相同的问题.当我在拆分之前输出字符串时,它显示特殊字符就好了.

                  As you can see, it not only messes up the characters, but it also duplicates them in str_split process. I've tried several ways to split the string, but they all have the same issue. When I output the string before the split, it shows the special characters just fine.

                  这是我的 dbConn.php 代码:

                  This is my dbConn.php code:

                  //需要配置文件:require_once('config.inc.php');

                  // Require config file: require_once('config.inc.php');

                  // Start PDO connection:
                  $dbHandle = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf-8", $dbUser, $dbPass);
                  $dbHandle -> exec("SET CHARACTER SET utf8");
                  
                  // Set error reporting:
                  $dbHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
                  

                  这是我用来从数据库中获取并循环的代码:

                  And this is the code that I use to fetch from the database and loop:

                  // Require files:
                  require_once('dbConn.php');
                  
                  // Get random artist:
                  $artist = $dbHandle->query("SELECT * FROM ".ARTIST_TABLE." WHERE id = 11 ORDER BY RAND() LIMIT 1");
                  $artist->setFetchMode(PDO::FETCH_OBJ);
                  $artist = $artist->fetch();
                  var_dump($artist->name);
                  
                  // Split name:
                  $artistChars = str_split($artist->name);
                  

                  我使用 utf-8 连接,我的 php 文件是 utf-8 没有 BOM 并且此页面上没有其他特殊字符共享此问题.可能有什么问题,或者我做错了什么?

                  I'm connecting with utf-8, my php file is utf-8 without BOM and no other special characters on this page share this issue. What could be wrong, or what am I doing wrong?

                  推荐答案

                  str_split 不适用于 multi-byte 字符,它只会返回第一个字节 - 从而使您的字符无效.你可以使用 mb_split.

                  这篇关于在 UTF-8 编码的字符串上使用 str_split的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:utf-8中的php正则表达式单词边界匹配 下一篇:Laravel 中的“格式错误的 UTF-8 字符,可能编码不

                  相关文章

                  最新文章

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

                    2. <legend id='TZ6P6'><style id='TZ6P6'><dir id='TZ6P6'><q id='TZ6P6'></q></dir></style></legend>

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