472,378 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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 11777
Dormilich
8,658 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,658 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,658 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

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

Similar topics

4
by: kinne | last post by:
The following code is supposed to reverse the date in "yyyy-mm-dd" format, but it produces different results in Firefox 1.0 and in Internet Explorer 6SP1. In Firefox, the result is correct...
16
by: nicolas | last post by:
Aim: scanf a number what random digit,split this number; for example: input "5680" output "5,6,8,0"; input "45" output "4,5";
11
by: Jay | last post by:
Hey Guys, I need an algorithm/formula to do the following: I have two 32-bit timers cascaded to form a 64-bit timer, max value per timer(50sec). This is the way they work: value | timer1 |...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
2
by: nani | last post by:
Hi friends I am creating one web application in that i take one hyperlink control.when i click hyperlink control.how to split the page in two parts and display the result in that secound part If...
13
by: Chaim Krause | last post by:
I am unable to figure out why the first two statements work as I expect them to and the next two do not. Namely, the first two spit the sentence into its component words, while the latter two...
5
by: Alex Hoilo | last post by:
Hello, I'm a novice to VBA and need to import raw data into an access database. I created the table, it contains 87 fields, all numeric. The data I have looks like this: ...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.