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 :)
9 11999
you can use a RegEx (preg_replace()).
@Dormilich
preg_split() would be better ;)
And str_split() better still - pay attention to the optional 'length' parameter.
@Markus
that depends on the desired output.
@Dormilich
I don't think the output is in question - the OP asked for his string to be split.
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.
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: - $number = "922888222";
-
$array = str_split($number,3); // Splits the number into a new element every 3 characters
-
echo( $array[0],' ', $array[1],' ', $array[2] ); // Ouputs each element with a space in between
@zorgi
Great idea! Looks like it would work.
- /**
-
* @param float $size - the memory size to format
-
* @param bool $is_add_commas (optional) - saperete every 3 digits from the end so 56789 will be "56,789".
-
* @param bool $is_full_description (optional) - use *Bytes instead of *b (GigaBytes instead of gb, etc...).
-
* @param int $digits (optional) - number of digits decimal point, to limit.
-
* @return string
-
* @author http://icompile.eladkarako.com
-
*/
-
function human_readable_memory_sizes($size, $is_add_commas = false, $is_full_description = false, $digits = 20) {
-
$unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
-
$unit_full = array('Bytes', 'KiloByte', 'MegaBytes', 'GigaBytes', 'TeraBytes', 'PetaBytes');
-
-
$out = $size / pow(1024, ($i = floor(log($size, 1024))));
-
-
$out = sprintf("%." . $digits . "f", $out);
-
-
$out = !$is_add_commas ? $out : preg_replace_callback('/(\d)(?=(\d{3})+$)/', function ($arr) {
-
return isset($arr[0]) ? "$arr[0]," : "";
-
}, $out);
-
-
$out .= ' ' . (!$is_full_description ? $unit[ $i ] : $unit_full[ $i ]);
-
-
return $out;
-
}
-
the preg_replace_callback is what you want (in PHP),
for JavaScript your can use: - function addCommas(t) {
-
return String(t).replace(/(\d)(?=(\d{3})+$)/g, "$1,")
-
}
taken from:
first one: http://icompile.eladkarako.com/php-s...ar-expression/
second one: http://icompile.eladkarako.com/javas...ar-expression/
Elad.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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";
|
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 |...
|
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...
|
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...
|
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...
|
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:
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |