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