What is the cleanest way to convert a 5-7 digit number to xxx / xxx / xxx format in php?

I have sets of 5, 6 and 7 digits. I need them to display in the 000/000/000 format. For example:

12345 will display as 000/012/345

and

9876543 will display as 009/876/543

I know how to do this in a dirty way, including a series of if / else statements and strlen functions, but there should be a cleaner way using a regex that Im doesn't see.

+4
source share
7 answers

sprintf and modulo are one option

function formatMyNumber($num) { return sprintf('%03d/%03d/%03d', $num / 1000000, ($num / 1000) % 1000, $num % 1000); } 
+16
source
 $padded = str_pad($number, 9, '0', STR_PAD_LEFT); $split = str_split($padded, 3); $formatted = implode('/', $split); 
+5
source

You requested a regex solution, and I like playing with them, so here is a regex solution! I show it only for educational (and fun) purpose, just use Adam's solution, clean, readable and quick.

 function FormatWithSlashes($number) { return substr(preg_replace('/(\d{3})?(\d{3})?(\d{3})$/', '$1/$2/$3', '0000' . $number), -11, 11); } $numbers = Array(12345, 345678, 9876543); foreach ($numbers as $val) { $r = FormatWithSlashes($val); echo "<p>$r</p>"; } 
+3
source

Ok, people throw things, so me too!

number_format would be great because it accepts a thousands separator, but it does not make zero zeros like sprintf and the like. So, here is what I came up with for one liner:

 function fmt($x) { return substr(number_format($x+1000000000, 0, ".", "/"), 2); } 
+1
source

Minor improvement in PhiLho's offer:

You can avoid substr by changing the regex to:

 function FormatWithSlashes($number) { return preg_replace('/^0*(\d{3})(\d{3})(\d{3})$/', '$1/$2/$3', '0000' . $number); } 

I also deleted ? after each of the first two capture groups, because when a 5, 6, or 7-digit number is given (as indicated in the question), it will always have at least 9 digits to work with. If you want to protect against the possibility of getting a smaller input number, run the regular expression with '000000000'. $ number .

Alternatively you can use

 substr('0000' . $number, -9, 9); 

and then merge the slash in appropriate places with substr_replace , which I suspect might be the fastest way to do this (no need to run regular expressions or perform splitting), but it really is just pointless optimization, since any of the solutions presented will be - still much faster than establishing a network connection to the server.

+1
source

That would be the way I would write it if using Perl 5.10 .

 use 5.010; sub myformat(_;$){ # prepend with zeros my $_ = 0 x ( 9-length($_[0]) ) . $_[0]; my $join = $_[1] // '/'; # using the 'defined or' operator `//` # m// in a list context returns ($1,$2,$3,...) join $join, m/ ^ (\d{3}) (\d{3}) (\d{3}) $ /x; } 

Tested with

 $_ = 11111; say myformat; say myformat(2222); say myformat(33333,';'); say $_; 

returns:

  000/011/111
 000/002/222
 000; 033; 333
 11111

Back ported to Perl 5.8 :

 sub myformat(;$$){ local $_ = @_ ? $_[0] : $_ # prepend with zeros $_ = 0 x ( 9-length($_) ) . $_; my $join = defined($_[1]) ? $_[1] :'/'; # m// in a list context returns ($1,$2,$3,...) join $join, m/ ^ (\d{3}) (\d{3}) (\d{3}) $ /x; } 
+1
source

Here is how I would do it in python (sorry, I don't know PHP either). I am sure you can convert it.

 def convert(num): #num is an integer a = str(num) s = "0"*(9-len(a)) + a return "%s/%s/%s" % (s[:3], s[3:6], s[6:9]) 

It just fills a number with a length of 9, and then splits the substrings. At the same time, it seems that the modular answer is slightly better.

0
source

All Articles