php语言 百分网手机站

php指定长度分割字符串str-split函数如何实现

时间:2020-10-22 16:05:17 php语言 我要投稿

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指定长度分割字符串str-split函数如何实现】相关文章:

1.PHP如何获取中文字符串长度

2.php中利用explode函数分割字符串到数组

3.PHP的字符串函数

4.php字符串分割的详解

5.php的字符串常用函数

6.获取php数组长度函数的方法

7.PHP常用字符串相关函数

8.php截取指定长度中文字符的技巧

9.C++的字符串分割函数的使用详解