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

更新时间:2021-06-12    来源:php函数    手机版     字体:

【www.bbyears.com--php函数】

示例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"

}

本文来源:http://www.bbyears.com/jiaocheng/123219.html