php以逗号切割换行,第10个逗号后的PHP换行符

这篇博客展示了如何在PHP中将一个包含多个数字的长字符串分割成每10个元素一行。通过使用explode、implode和array_chunk函数,可以有效地对字符串进行处理,方便阅读和操作。示例代码详细解释了实现过程。
摘要由CSDN通过智能技术生成

我需要分割以下字符串:

333 ,351 ,359 ,360 ,370 ,371 ,385 ,492 ,512 ,514 ,528 ,539 ,546 ,628 ,630 ,634 ,636 ,702 ,706 ,709 ,710 ,715 ,718 ,719 ,763 ,770 ,803 ,822 ,823

分隔成几行,在最后一个第十个逗号后换行,如果有意义的话?

所以看起来像这样:

333 ,351 ,359 ,360 ,370 ,371 ,385 ,492 ,512 ,514 ,

528 ,539 ,546 ,628 ,630 ,634 ,636 ,702 ,706 ,709 ,

710 ,715 ,718 ,719 ,763 ,770 ,803 ,822 ,823

解决方法:

请检查注释以获取解释:-

error_reporting(E_ALL); // check if any error occur

ini_set('display_errors',1); // display error

$string = '333 ,351 ,359 ,360 ,370 ,371 ,385 ,492 ,512 ,514 ,528 ,539 ,546 ,628 ,630 ,634 ,636 ,702 ,706 ,709 ,710 ,715 ,718 ,719 ,763 ,770 ,803 ,822 ,823'; // original string

echo $string; // echo original string

$array = explode(',',$string); // explode string with comma to make it array

echo "

";print_r($array); // print array 
 

$chunked_array = array_chunk($array,10); // chunk array to each 10 counts and make a multidimensional array

$new_string = ''; // create a new empty string

foreach ($chunked_array as $chunked_ar){ // iterate through multi-dimensional array

$new_string .= implode(',',$chunked_ar)."\n"; // convert each array to string and add new line and assign it to new string variable

}

echo $new_string; // echo new variable.

?>

注意:添加error_reporting代码(在

标签:comma,split,string,php

来源: https://codeday.me/bug/20191027/1941911.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值