PHP:如何拆分UTF-8字符串? [英] PHP: How to split a UTF-8 string?

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

问题描述

我有以下代码不适用于UTF-8字符.我该如何解决?

I have the following code which is not working with UTF-8 characters. How can I fix it?

$seed = preg_split('//u', $seed, -1, PREG_SPLIT_NO_EMPTY);
$seed = str_split('АБВГДЕЖЗ'); // and any other characters

shuffle($seed); // probably optional since array_is randomized; this may be redundant
$code = '';
foreach (array_rand($seed, 5) as $k) $md5_hash .= $seed[$k];

//We don't need a 32 character long string so we trim it down to 5 
$security_code = $code;

我已经尝试过以下代码:

I have tried this code:

$seed = preg_split('//u', $seed, -1, PREG_SPLIT_NO_EMPTY);

但它仍然无法正常工作.

but it is still not working.

推荐答案

您必须先创建变量$seed并为其提供字符串值,然后才能将其用作preg_split的第二个参数:

You must create the variable $seed and give it a string value before you can use it as the second parameter of preg_split:

$seed = 'АБВГДЕЖЗ';
$seed = preg_split('//u', $seed, -1, PREG_SPLIT_NO_EMPTY);

print_r($seed)的输出将是:

Array
(
    [0] => А
    [1] => Б
    [2] => В
    [3] => Г
    [4] => Д
    [5] => Е
    [6] => Ж
    [7] => З
)

我希望您的其余代码都能正常工作.

I hope the rest of your code will work just fine.

这篇关于PHP:如何拆分UTF-8字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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