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 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 22, 2013 Share Posted May 22, 2013 (edited) 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; Edited May 22, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
hyster Posted May 22, 2013 Author Share Posted May 22, 2013 cheers that worked a treat Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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,'', ' '); Quote Link to comment Share on other sites More sharing options...
Solution DavidAM Posted May 23, 2013 Solution 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. Quote Link to comment 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 Quote Link to comment 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.