Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP在n个逗号后换行_Php - Fatal编程技术网

PHP在n个逗号后换行

PHP在n个逗号后换行,php,Php,我想在n个逗号后插入新行。 例如,我得到了这个值:3853863873883893903913923939439539639739839940040140240340440640740949494104114124134144154174184194204214224425426 我怎么能重复它们,但每5个逗号都应该有一个换行符 385386387388389, 390,391,392,393,394, 395,396,397,398,399, 400,401,402,403,404, 405,

我想在n个逗号后插入新行。 例如,我得到了这个值:
3853863873883893903913923939439539639739839940040140240340440640740949494104114124134144154174184194204214224425426

我怎么能重复它们,但每5个逗号都应该有一个换行符

385386387388389,
390,391,392,393,394,
395,396,397,398,399,
400,401,402,403,404,
405,406,407,408,409,
410,411,412,413,414,
415,416,417,418,419,
420,421,422,423,424,
425426

像这样试试

  • 您可以使用逗号分解字符串,并每5次检查一次 位置应该有一个断线

  • 你可以用带5的分频键来检查它。(也就是说)它会给你一个 0的余数

  • 请注意,键从0开始,所以我添加了(键+1),使其从1开始

    $string=“3853863873893903913923933994395396397398399400401402403404406407408409410411412414415416417418419420421422424425426”;
    $stringexplode=explode(“,”,$string);
    $countstringexplode=计数($stringexplode);
    foreach($stringexplode作为$key=>$val)
    {
    $keyIncrement=$key+1;
    echo$val.($countstringexplode==$keyIncrement?”:“,”;
    如果(($keyIncrement)%5==0)
    回声“
    ”; } ?>
  • 这里有一个方法:

    // Get all numbers
    $numbers = explode(',', $str);
    // Split into groups of 5 (n)
    $lines = array_chunk($numbers, 5);
    // Format each line as comma delimited
    $formattedLines = array_map(function ($row) { return implode(',', $row); }, $lines);
    // Format groups into new lines with commas at the end of each line (except the last)
    $output = implode(",\n", $formattedLines);
    
    试试这个

    <?php
    //Start //Add this code if your values in string like that
    $string = "385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426";
    $string_array = explode(',', $string);
    //End //Add this code if your values in string like that
    
    //If you have values in array then direct use below code skip above code and replace $string_array variable with yours 
    
    end($string_array);        
    $last = key($string_array);
    
    foreach ($string_array as $key => $value) {
        if($last==$key){
            echo $value;
        }else{
            echo $value.',';
        }
        if(($key+1)%5==0){
            echo "<br />";
        }
    }
    
    ?>
    
    
    
    你试过什么吗?你应该试着至少发布一些代码,这表明你已经尝试了一些东西,而不是仅仅发布问题来获得答案。你可以删除
    ,”array\u map()
    中调用code>并将其添加到最后一位
    内爆(“,\n”
    我也尝试过使用explode的最佳方法之一,但无法对数组项进行计数。这就像一个charme
    <?php
    //Start //Add this code if your values in string like that
    $string = "385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426";
    $string_array = explode(',', $string);
    //End //Add this code if your values in string like that
    
    //If you have values in array then direct use below code skip above code and replace $string_array variable with yours 
    
    end($string_array);        
    $last = key($string_array);
    
    foreach ($string_array as $key => $value) {
        if($last==$key){
            echo $value;
        }else{
            echo $value.',';
        }
        if(($key+1)%5==0){
            echo "<br />";
        }
    }
    
    ?>