tags:

views:

61

answers:

6

I am working with a membership database which records all accounts in a six digit format however, some users will be using a sub-six digit format due to older conventions. So I want to be able to accept a sub-six digit number and prefix zeroes to the beginning of it.

e.g. user enters number 1234, I want PHP to format it to become 001234. user enters number 123, I want PHP to format it to become 000123.

+1  A: 
$padded_num = str_pad($num, 6, 0, STR_PAD_LEFT);

Padded numbers must be represented as strings for output.

Gazler
It says on http://php.net/manual/en/function.str-pad.php that the third parameter (the 0) should be a string (enclosed in quotes), like in the example below. While the code above seems to work, it's probably best to use the code below.
matsolof
A: 

You can use str_pad function:

str_pad($number, 6, "0", STR_PAD_LEFT);
Parkyprg
+6  A: 

You can use PHPs sprintf()-function:

$formattedNumber = sprintf('%06d', $unformattedNumber);
elusive
+2  A: 

You can try sprintf

<?php
$id= sprintf("%06d", $id);
?> 
Extrakun
A: 
$new_num = str_pad($Num, 6, "0", STR_PAD_LEFT);
jatt
+2  A: 

Too Easy

str_pad($input, 6, "0", STR_PAD_LEFT);
Andrew Dunn