当前位置:才华咖 > 互联网计算机 > 计算机 > php语言 > php指定长度分割字符串str-split函数如何实现
手机版

php指定长度分割字符串str-split函数如何实现

来源:才华咖 阅读:2.69W 次

导语: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"

}

本文链接:https://www.caihuaka.com/jsjzs/php/q90vqd.html

Copyright © 2024. 才华咖 All right reserved. 浙ICP备20120231号-3

文字美图素材,版权属于原作者。部分文章内容由网友提供推送时因种种原因未能与原作者联系上,若涉及版权问题,敬请原作者联系我们,立即处理。