【php】php分隔字符串为数组
工作中会经常分隔字符串为数组,我们可以用php内置函数str_split(),可是有时候字符串中包含中文,切割后会乱码,比如
print_r(str_split('dw氛围fesf',3));
输出
Array
(
[0] => php
[1] => �
[2] => ��
[3] => ��
[4] => ��
[5] => ��
[6] => �!!
[7] => !
) 为了能处理多字节字符串
下面函数可以实现
function mbStringToArray($str) {
if(empty($str)){
return false;
}
$len = mb_strlen($str);
$array = array();
for($i = 0; $i<$len; $i++) {
$array[] = mb_substr($str, $i, 1);
}
return $array;
}
/**
* @param str $str
* @param int $length 截取长度
* @param boole $byte 是否按字节分隔 false 按字符数分隔
* @return array
*/
function _str_split($str,$length,$byte=false){
if(mb_strwidth($str) == 1 || empty($str)){
return $str;
}
if($encoding = mb_detect_encoding($str, null, true) === false ){
return str_split($str, $length);
}
$utf8_str = mb_convert_encoding($str, 'utf8', $encoding);
if($byte){
$line = '';
$split_arr = [];
foreach (preg_split('//u', $utf8_str,-1,PREG_SPLIT_NO_EMPTY ) as $char) {
$width = mb_strwidth($line.$char,'utf8');
if($width <= $length){
$line .= $char;
continue;
}
$split_arr[] = str_pad($line, $width);
$line = $char;
}
return $split_arr;
}else{
$str_arr = mbStringToArray($str);
if($str_arr){
$chunk_index = 0;
$k_index = 0;
$line = '';
$chunks = [];
foreach ($str_arr as $key=>$val){
$line .= $val;
$chunks[$k_index] = $line;
if ($chunk_index++ == $length-1) {
$line = '';
$k_index++;
$chunk_index = 0;
}
}
}
return $chunks;
}
}
执行
print_r(_str_split($str,3,false));
输出
Array
(
[0] => php
[1] => 开发
[2] => 者中心
[3] => !!!
)
【php】php分隔字符串为数组的更多相关文章
- [Shell] 分隔字符串为数组
#!/bin/bash tmp="test,girl,boy,love" OLD_IFS="$IFS" IFS="," arr=($a) I ...
- mysql下将分隔字符串转换为数组
MySQL存储过程可以用于分割字符串,下面就为您详细介绍这种MySQL存储过程的用法,供您参考学习之用. 现有一段字符串,如apple,banana,orange,pears,grape,要把它按照逗 ...
- SQL自定义函数split分隔字符串
SQL自定义函数split分隔字符串 一.F_Split:分割字符串拆分为数据表 Create FUNCTION [dbo].[F_Split] ( @SplitString nvarchar(max ...
- shell入门笔记2:字符串、数组、echo与printf
说明: 本文是关于http://c.biancheng.net/cpp/shell/的相关笔记 shell字符串 字符串可以用单引号,也可以用双引号,也可以不用引号. 1 #!/bin/bash 2 ...
- (js) 字符串和数组的常用方法
JS中字符串和数组的常用方法 JS中字符串和数组的常用方法 js中字符串常用方法 查找字符串 根据索引值查找字符串的值 根据字符值查找索引值 截取字符串的方法 字符串替换 字符串的遍历查找 字符串转化 ...
- javascript中字符串与数组互转的方法分享
说明:1.join()方法:用于把数组中的所有元素放入一个字符串,元素是通过指定的分隔符进行分隔的.指定分隔符方法join("$");其中$可以是任意字符2.split()方法:用 ...
- 【Java面试题】17 如何把一个逗号分隔的字符串转换为数组? 关于String类中split方法的使用,超级详细!!!
split 方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator],[limit])参数:stringObj 必选项.要被分解的 ...
- ios 字符串处理:截取字符串、匹配字符串、分隔字符串
1.截取字符串 NSString*string =@"sdfsfsfsAdfsdf";string = [string substringToIndex:7];//截取掉下标7之后 ...
- C#分隔字符串时遭遇空值
在C#中分隔字符串时,按特定字符进行分隔的时候可能会遇到空值,如何我现在传入的是Id的字符串,如:"1501,1502,1503,,1505",以逗号分隔,由于各种原因,导致传入的 ...
随机推荐
- Docker : Tomcat Clustering with Load Balancer (Tomcat and Nginx)
Tomcat Clustering Series Part 5 : NginX as Load Balancer - Ramki Technical Bloghttps://www.ramkitech ...
- nginx之快速查找配置文件
nginx的配置放在nginx.conf文件中,一般我们可以使用以下命令查看服务器中存在的nginx.conf文件. locate nginx.conf /usr/local/nginx/conf ...
- ios点击输入框,界面放大解决方案
当我们编写的input宽度没有占满屏幕宽度,而且又没有申明meta,就会出现点击输入框,界面放大这个问题. 下面我直接给出解决方案: <meta name="viewport" ...
- [转帖]linux sed命令
linux sed命令就是这么简单 https://www.cnblogs.com/wangqiguo/p/6718512.html 用到的最多的就是一个sed -i 's/nn/mm/' 的命令了. ...
- 1244. Minimum Genetic Mutation
描述 A gene string can be represented by an 8-character long string, with choices from "A", ...
- Could not render e, see the console.
错误截图: 解决: 在application.properties中开启swagger swagger2.enable=true
- tomcat优化实例
———————————————————————————————————— 一.运行模式优化 修改tomcat运行模式为nio<Connector port="80" prot ...
- Python:matplotlib绘制直方图
使用hist方法来绘制直方图: 绘制直方图,最主要的是一个数据集data和需要划分的区间数量bins,另外你也可以设置一些颜色.类型参数: plt.hist(np.random.randn(1 ...
- JDK 12 & JAVA
JDK 12 & JAVA js style https://github.com/winterbe https://winterbe.com/posts/2018/09/24/java-11 ...
- 在Hmtl页面中只让其中单独的一个div隐藏滚动条但是仍可滚动浏览下边的内容
<style> .box ::-webkit-scrollbar {width: 0px;} </style> <div class="box"> ...