aebstract Posted December 29, 2011 Share Posted December 29, 2011 I have a string that is unformatted and has a lot of commas in it. How would I add a \n linebreak after every 4th comma? Link to comment https://forums.phpfreaks.com/topic/254016-add-x-every-nth-of-needle/ Share on other sites More sharing options...
Muddy_Funster Posted December 29, 2011 Share Posted December 29, 2011 Not sure this will work, but it's where I would start: $str_array = explode(",", $unformatedString) for ($i = 1; $i <= array_count($str_array); $i++){ if (isnumeric($i / 4) && $str_array[$i-1] != '\n'){ array_splice($str_array, $i-1, 0, '\n'); $i = 1 } } $unformatedReturn = implode(",", $str_array); Link to comment https://forums.phpfreaks.com/topic/254016-add-x-every-nth-of-needle/#findComment-1302168 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.