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? Quote 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); Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.