本文实例讲述了php指定长度分割字符串str_split函数用法。分享给大家供大家参考,具体如下:
示例1:
1
2
|
$str = 'abcdefgh' ; $arr = str_split ( $str ,2); |
运行结果如下:
1
2
3
4
5
6
7
8
9
10
|
array (4) { [0]=> string(2) "ab" [1]=> string(2) "cd" [2]=> string(2) "ef" [3]=> string(2) "gh" } |
示例2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
$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 ); |
运行结果如下:
1
2
3
4
5
6
7
8
9
10
|
array (4) { [0]=> string(2) "ab" [1]=> string(2) "cd" [2]=> string(2) "ef" [3]=> string(2) "gh" } |
希望本文所述对大家PHP程序设计有所帮助。