第10个逗号后的PHP换行符 [英] PHP newline after 10th comma

查看:51
本文介绍了第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?

所以看起来像这样:

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

推荐答案

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

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

输出:- https://eval.in/557389

注意:添加error_reporting代码(在<?php 之后的前两行)始终是找出错误并加以解决的好方法.谢谢.

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

这篇关于第10个逗号后的PHP换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆