hyster Posted May 22, 2013 Share Posted May 22, 2013 I want to separate a number into sections of 3 to make it easier to read 1234567890 into 123 456 789 0. the number is not going to be a set length explode (as far as I can tell) uses a separator to do this IE: ; or - and I carnt find another function to do this thx for any help Link to comment https://forums.phpfreaks.com/topic/278297-separate-variable-into-sections-of-3/ Share on other sites More sharing options...
jazzman1 Posted May 22, 2013 Share Posted May 22, 2013 Try, $int = 1234567890; $value = chunk_split ( $int , 3, "\n"); echo $value; EDIT: If you are on windows machine add \r (not sure about that) $int = 1234567890; $value = chunk_split ( $int , 3, "\r\n"); echo $value; Link to comment https://forums.phpfreaks.com/topic/278297-separate-variable-into-sections-of-3/#findComment-1431706 Share on other sites More sharing options...
hyster Posted May 22, 2013 Author Share Posted May 22, 2013 cheers that worked a treat Link to comment https://forums.phpfreaks.com/topic/278297-separate-variable-into-sections-of-3/#findComment-1431708 Share on other sites More sharing options...
hyster Posted May 23, 2013 Author Share Posted May 23, 2013 is there a way to reverse the way the function works? start the count from the right instead of the left? 1234 chunk_split = 123 4 // looks a bit daft 1234 chunk_split /reverse = 1 234 Link to comment https://forums.phpfreaks.com/topic/278297-separate-variable-into-sections-of-3/#findComment-1431816 Share on other sites More sharing options...
jazzman1 Posted May 23, 2013 Share Posted May 23, 2013 is there a way to reverse the way the function works? start the count from the right instead of the left? 1234 chunk_split = 123 4 // looks a bit daft 1234 chunk_split /reverse = 1 234 Yes, I am a RegEx fan. http://www.regular-expressions.info/reference.html Link to comment https://forums.phpfreaks.com/topic/278297-separate-variable-into-sections-of-3/#findComment-1431817 Share on other sites More sharing options...
hyster Posted May 23, 2013 Author Share Posted May 23, 2013 im properly being brain dead but I carnt find anything relevant to what im after. as far as I can tell I can search for specific numbers but not split them the way I want Link to comment https://forums.phpfreaks.com/topic/278297-separate-variable-into-sections-of-3/#findComment-1431846 Share on other sites More sharing options...
Muddy_Funster Posted May 23, 2013 Share Posted May 23, 2013 strlen() and substr() could also be used to do it. Link to comment https://forums.phpfreaks.com/topic/278297-separate-variable-into-sections-of-3/#findComment-1431848 Share on other sites More sharing options...
mac_gyver Posted May 23, 2013 Share Posted May 23, 2013 $num = "1234567890"; echo number_format($num,0,'', ' '); Link to comment https://forums.phpfreaks.com/topic/278297-separate-variable-into-sections-of-3/#findComment-1431851 Share on other sites More sharing options...
DavidAM Posted May 23, 2013 Share Posted May 23, 2013 If the value is actually a numeric value, you can use number_format, specifying a space as the thousands separator. If the value is a string of characters limited to the digits 0 - 9, there are a couple of options: 1) Reverse the string, split it, then reverse the result; 2) Pad the string so the length is a multiple of 3, split it, remove the padding. # An actual number $int = 123456789; echo number_format($int, 0, '.', ' '); # A String - Reverse it, split it, reverse the result $rev = strrev($int); $split = trim(chunk_split($rev, 3, ' ')); echo strrev($split); # A String - Extend it, split it, remove padding $padded = str_repeat('?', 3 - (strlen($int) % 3)) . $int; $split = chunk_split($padded, 3, ' '); echo trim(str_replace('?', '', $split)); Of course, there is the brute-force method using a loop to build the output one character at a time, but it's much more code, and I'm too lazy to write it right now. Link to comment https://forums.phpfreaks.com/topic/278297-separate-variable-into-sections-of-3/#findComment-1431852 Share on other sites More sharing options...
hyster Posted May 23, 2013 Author Share Posted May 23, 2013 thx mac_gyver that was exactly what I was looking for. sorry if I wasn't clear guys and thx for all ur help Link to comment https://forums.phpfreaks.com/topic/278297-separate-variable-into-sections-of-3/#findComment-1431870 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.