$str = 'abcdefgh';
$arr = str_split($str,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);
本文介绍了一种使用PHP的str_split函数来分割字符串的方法,并通过手写代码实现将字符串按指定长度进行分组的功能。该方法适用于需要对字符串进行批量处理的应用场景。
1162

被折叠的 条评论
为什么被折叠?



