php指定长度分割字符串str_split函数用法示例

作者:design321 时间:2023-07-08 23:25:51 

本文实例讲述了php指定长度分割字符串str_split函数用法。分享给大家供大家参考,具体如下:

示例1:


$str = 'abcdefgh';
$arr = str_split($str,2);

运行结果如下:


array(4) {
[0]=>
string(2) "ab"
[1]=>
string(2) "cd"
[2]=>
string(2) "ef"
[3]=>
string(2) "gh"
}

示例2:


$str = 'abcdefgh';
$arr = str_split($str);
$i = 0;
$limit = 3;
$num = count($arr);
while($i <= $num-1){
 $temp = array();
 $for_countbu = ($num-$i) >= $limit ? $limit : $num - $i;
 for($j = 0; $j < $for_countbu; ++$j)
 {
   $temp[] = $arr[$i];
   ++$i;
 }
 $one = implode('',$temp);
 $result[] = $one;
}
print_r($result);

运行结果如下:


array(4) {
[0]=>
string(2) "ab"
[1]=>
string(2) "cd"
[2]=>
string(2) "ef"
[3]=>
string(2) "gh"
}

希望本文所述对大家PHP程序设计有所帮助。

标签:php,str,split,函数
0
投稿

猜你喜欢

  • Python实现模拟时钟代码推荐

    2023-08-03 05:26:09
  • 简单了解什么是神经网络

    2023-10-11 22:26:34
  • 使用Python自动生成HTML的方法示例

    2022-08-12 12:12:18
  • python使用epoll实现服务端的方法

    2021-05-16 22:52:34
  • sql ntext数据类型字符替换实现代码

    2011-09-30 11:08:00
  • 微信小程序实现触底加载

    2024-04-23 09:30:21
  • python实现多人聊天室

    2022-09-02 18:56:21
  • python使用requests.session模拟登录

    2022-09-12 19:17:24
  • MySQL事务处理与应用简析

    2024-01-21 09:01:24
  • Python入门教程(一)Python简单介绍

    2023-10-25 03:19:16
  • Python使用多进程运行含有任意个参数的函数

    2023-10-14 23:22:12
  • GO CountMinSketch计数器(布隆过滤器思想的近似计数器)

    2024-02-17 06:12:40
  • javascript将中国数字格式转换成欧式数字格式的简单实例

    2024-05-09 10:20:21
  • 编写安全的SQL Server扩展存储过程

    2008-11-25 11:16:00
  • python argparse传入布尔参数false不生效的解决

    2023-07-03 16:12:20
  • Python数据类型详解(二)列表

    2021-10-26 08:47:11
  • node.js应用后台守护进程管理器Forever安装和使用实例

    2024-05-03 15:36:48
  • pytorch中torch.topk()函数的快速理解

    2023-09-07 10:21:52
  • go语言csrf库使用实现原理示例解析

    2023-08-07 03:34:38
  • 谈切图优化加速图片显示—淘宝实例

    2008-01-30 12:24:00
  • asp之家 网络编程 m.aspxhome.com