沃梦达 / 编程问答 / php问题 / 正文

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

Using str_split on a UTF-8 encoded string(在 UTF-8 编码的字符串上使用 str_split)

本文介绍了在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 UTF-8 编码的字符串上使用 str_split

基础教程推荐