将中文字符串分割为数组 解决str_split中文乱码php

不言
Release: 2023-02-28 15:54:01
Original
3683 people have browsed it

首先来介绍str_split()这个函数;

它的作用是将字符串分割为数组;

例如:

$str='abcde';
str_plite($str);
打印结果如下:
Array(    [0] => a    [1] => b    [2] => c    [3] => d    [4] => e)
Copy after login

看似很好用的样子;但是作为中国程序员;不可避免的要和中文打交道;

这时候再用str_splite就会悲剧的发现乱码了;;;

不要怕;preg_splite可以拯救这个问题;

当然是需要配合正则使用的;

/** 
* 将字符串分割为数组     
* @param  string $str 字符串 
* @return array       分割得到的数组 
*/
function mb_str_split($str){    
return preg_split(&#39;/(?<!^)(?!$)/u&#39;, $str );
}
$str=&#39;白俊遥博客&#39;;
mb_str_split($str);
打印结果如下:
Array(    [0] => 白    [1] => 俊    [2] => 遥    [3] => 博    [4] => 客)
Copy after login

打完;收工;

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!