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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

design321   2021-03-24 我要评论
想了解php指定长度分割字符串str_split函数用法示例的相关内容吗,design321在本文为您仔细讲解php指定长度分割字符串str_split函数的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:php,指定长度,分割,字符串,str_split,函数,下面大家一起来学习吧。

本文实例讲述了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程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们