469,271 Members | 1,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

how split number 3 parts

hi,
i have a number with 9 digits lenght
and i want split this number: 922888222
into this: 922 888 222
how can i do that?
thanks a lot for your help :)
Sep 30 '09 #1
9 11303
Dormilich
8,651 Expert Mod 8TB
you can use a RegEx (preg_replace()).
Sep 30 '09 #2
Markus
6,050 Expert 4TB
@Dormilich
preg_split() would be better ;)

And str_split() better still - pay attention to the optional 'length' parameter.
Sep 30 '09 #3
Dormilich
8,651 Expert Mod 8TB
@Markus
that depends on the desired output.
Sep 30 '09 #4
Markus
6,050 Expert 4TB
@Dormilich
I don't think the output is in question - the OP asked for his string to be split.
Sep 30 '09 #5
Dormilich
8,651 Expert Mod 8TB
the OP didnít mention to split it into an array. you know how loose the meaning of a word can be in this forum.
Sep 30 '09 #6
TheServant
1,168 Expert 1GB
I am presuming that it is a phone number or the like that the OP wants formatted. How I would do it, not saying Dorms way won't work or be better, but str_split() and then a simple echo with spaces. It can be done in a loop if the length of the original number is variable, but I think this will do:
Expand|Select|Wrap|Line Numbers
  1. $number = "922888222";
  2. $array = str_split($number,3); // Splits the number into a new element every 3 characters
  3. echo( $array[0],' ', $array[1],' ', $array[2] ); // Ouputs each element with a space in between
Sep 30 '09 #7
zorgi
431 Expert 256MB
I would probably do it like this


Expand|Select|Wrap|Line Numbers
  1. number_format(922888222, 0, " ", " ");
  2.  
http://hr.php.net/number_format
Oct 1 '09 #8
TheServant
1,168 Expert 1GB
@zorgi
Great idea! Looks like it would work.
Oct 1 '09 #9
Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * @param float $size                - the memory size to format
  3.  * @param bool  $is_add_commas       (optional) - saperete every 3 digits from the end so 56789 will be "56,789".
  4.  * @param bool  $is_full_description (optional) - use *Bytes instead of *b (GigaBytes instead of gb, etc...).
  5.  * @param int   $digits              (optional) - number of digits decimal point, to limit.
  6.  * @return string
  7.  * @author http://icompile.eladkarako.com
  8.  */
  9. function human_readable_memory_sizes($size, $is_add_commas = false, $is_full_description = false, $digits = 20) {
  10.   $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
  11.   $unit_full = array('Bytes', 'KiloByte', 'MegaBytes', 'GigaBytes', 'TeraBytes', 'PetaBytes');
  12.  
  13.   $out = $size / pow(1024, ($i = floor(log($size, 1024))));
  14.  
  15.   $out = sprintf("%." . $digits . "f", $out);
  16.  
  17.   $out = !$is_add_commas ? $out : preg_replace_callback('/(\d)(?=(\d{3})+$)/', function ($arr) {
  18.     return isset($arr[0]) ? "$arr[0]," : "";
  19.   }, $out);
  20.  
  21.   $out .= ' ' . (!$is_full_description ? $unit[ $i ] : $unit_full[ $i ]);
  22.  
  23.   return $out;
  24. }
  25.  
the preg_replace_callback is what you want (in PHP),
for JavaScript your can use:
Expand|Select|Wrap|Line Numbers
  1. function addCommas(t) {
  2.   return String(t).replace(/(\d)(?=(\d{3})+$)/g, "$1,")
  3. }

taken from:
first one:
http://icompile.eladkarako.com/php-s...ar-expression/

second one:
http://icompile.eladkarako.com/javas...ar-expression/

Elad.
Aug 1 '14 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by nicolas | last post: by
19 posts views Thread by gk245 | last post: by
13 posts views Thread by Chaim Krause | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.