PHP在n个逗号后换行
我想在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,
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
像这样试试
$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 />";
}
}
?>