网络编程
位置:首页>> 网络编程>> php编程>> php指定长度分割字符串str_split函数用法示例

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

作者:design321  发布时间:2023-07-08 23:25:51 

标签: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程序设计有所帮助。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com