dongyuelian9602 2016-04-21 10:56
浏览 176
已采纳

10行逗号后的PHP换行符

I need to split the following 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

into separate lines where a new line begins after the last 10th comma, if that makes any sense?

So it looks like this:

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
  • 写回答

4条回答 默认 最新

  • drg5577 2016-04-21 11:13
    关注

    Please check the comments for explanation:-

     <?php
        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 "<pre/>";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)."
    "; // convert each array to string and add new line and assign it to new string variable
        }
        echo $new_string; // echo new variable.
      ?>
    

    Output:- https://eval.in/557389

    Note : Adding error_reporting code (first two lines after <?php) is always a very good practice to find out errors and resolve them. Thanks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了