【问题标题】:PHP newline after 10th comma第 10 个逗号后的 PHP 换行符
【发布时间】:2016-04-21 10:56:41
【问题描述】:

我需要拆分以下字符串:

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

在最后 10 个逗号之后换行开始的单独的行,如果这有意义吗?

所以它看起来像这样:

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

【问题讨论】:

  • 你尝试了什么?
  • 爆炸到,上的一个数组; array_chunk 分成 10 个块,再次遍历 , 内爆的数组,用换行符显示每个数组元素....$result = array_map(function($value) { return implode(',', $value); }, array_chunk(explode(',', $data), 10)); foreach($result as $line) { echo $line, PHP_EOL; }

标签: php string split comma


【解决方案1】:

请查看 cmets 以获得解释:-

 <?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 代码(&lt;?php 之后的前两行)始终是找出错误并解决它们的好习惯。谢谢。

【讨论】:

  • 这个工作很好!非常感谢您的详细回答!信息量很大!
  • 谁能说出为什么 -ve 标记了。即使它是正确的?
  • @A.L 写的注释告诉错误报告将始终对面临错误的程序员有所帮助。 Thanks 只是为了感谢您检查并将代码标记为已接受的答案。
  • 还有投反对票的人。也请给出原因,以便其他人知道他需要改进的地方。谢谢。
  • 我的问题不是关于错误报告,而是:- 字符,据我所知,冒号后面通常有一个空格,而不是减号。
【解决方案2】:

假设 $string 是你之前的字符串:

<?php

$myArray = explode(",", $string);
$newString = "";
$count = 0;
foreach ($myArray as $value)
{
  $newString = $newString.$value.",";
  $count++;
  if ($count==10)
  {
    $count=0;
    $newString = $newString."\n";
  }
}

?>

【讨论】:

    【解决方案3】:

    这样就可以了……

    $str = '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';
    
    $iCount = 0;
    foreach (explode(',',$str) as $iNum)
        echo $iNum.' ,'.(++$iCount % 10 == 0 ? '<br>' : '');
    

    【讨论】:

    • 不幸的是它没有做到这一点:我得到以下内容:333,, 351,, 359,, 360,, 370,, 371,, 385,, 492,, 512,, 514 ,,我使用以下代码对每个数字进行换行: $str = $row['Reference'] 。 ','; $iCount = 0; foreach (explode(',',$str) as $iNum) { if ($iCount % 10 == 0) echo '
      ';回声 $iNum.','; $iCount++; } `
    • 奇怪,我确实测试过这个!爆炸删除逗号,然后在输出每个项目时重新添加它们......无论如何都不要介意......
    【解决方案4】:

    你可以用单线做到这一点。

    preg_replace('/((.*?,){10})/', "$1\n", $text);
    

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      相关资源
      最近更新 更多