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

时间:2025-12-11 20:56:51 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函数如何实现】相关文章:

如何使用php自定义函数实现汉字分割替换03-19

PHP的字符串函数02-18

php自定义函数实现汉字分割替换01-06

PHP如何获取中文字符串长度02-15

如何运用PHP实现字符串逆序排列02-17

php字符串分割的详解11-01

php字符串截取函数01-08

php的字符串常用函数12-26

php中利用explode函数分割字符串到数组02-20