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

yipeiwu_com5年前PHP代码库

本文实例讲述了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数据结构与算法教程》、《php程序设计算法总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

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

相关文章

解析如何去掉CodeIgniter URL中的index.php

CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/下,你的下面的二级url就类似这样http://localhost/CodeIgniter...

php单例模式实现方法分析

本文实例讲述了php单例模式实现方法。分享给大家供大家参考。具体如下: <?php /** * @copyright 2013 maguowei.com * @au...

php实现的mongoDB单例模式操作类

本文实例讲述了php实现的mongoDB单例模式操作类。分享给大家供大家参考,具体如下: 看了好多mongo类都不尽人意。最后发现根本不需要自己封装类。php mongo 的扩展自带的方...

PHP更新购物车数量(表单部分/PHP处理部分)

表单部分: 复制代码 代码如下: <form action="?action=edit_num" method="post" name="car<?php $c_rs['id...

PHP socket 模拟POST 请求实例代码

我们用到最多的模拟POST请求几乎都是使用php curl来实现了,没考虑到PHP socket也可以实现,今天看到朋友写了一文章,下面我来给大家分享一下PHP socket模拟POST...