好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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函数用法的详细内容...

  阅读:44次