Num Class
The Num class provides additional formatting methods for working with numeric values
Configuration
The Num class provides different methods to format certain types of numbers.
Formats are strings where zero's represent the numeric parts of the returned value.
Note: when changing the num config, copy the file to app/config/num.php and change
the settings there to allow easy upgrading without loosing your custom settings.
phone |
string |
'(000) 000-0000'
|
The default phone number formatting.
|
smart_phone |
array |
array(
7 => '000-0000',
10 => '(000) 000-0000',
11 => '0 (000) 000-0000',
),
|
Phone number formatting based on the length of the phonenumber. array(length => formatting)
|
exp |
string |
'00-00'
|
Default formatting for credit card expiration date.
|
credit_card |
string |
'00-00'
|
Default formatting for credit card number masking.
|
bytes($size = 0)
The bytes method converts a (file size) number to a byte value. File sizes are
defined in the format: SB, where S is the size (1, 8.5, 300, etc.) and B is the byte unit
(K, MiB, GB, etc.). All valid byte units are defined in core/lang/en/byte_units.php
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$size |
string|int|float |
0
|
The string to convert. |
|
Returns |
float |
Example |
echo Num::bytes('200K'); // 204800
echo Num::bytes('5MiB'); // 5242880
echo Num::bytes('1000'); // 1000
echo Num::bytes('2.5GB'); // 2684354560
|
The format_bytes method converts a number of bytes to a human readable format.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$bytes |
int |
0
|
The number to format. |
$decimals |
int |
0
|
The number of decimals to round up to. |
|
Returns |
string|false |
Example |
echo Num::format_bytes('204800'); // 200 kB
echo Num::format_bytes('214901', 1); // 209.9 kB
echo Num::format_bytes('2249010', 1); // 2.1 MB
echo Num::format_bytes('badbytes'); // false
|
quantity($num, $decimals = 0)
The quantity method converts a number to a human readable format.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$num |
int |
required |
The number to format. |
$decimals |
int |
0
|
The number of decimals to round up to. |
|
Returns |
string|false |
Example |
echo Num::quantity(7000); // 7K
echo Num::quantity(7500); // 8K
echo Num::quantity(7500, 1); // 7.5K
echo Num::quantity('badnumber'); // false
|
The format method converts a number to a given format.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$string |
string |
''
|
The number to format. |
$format |
string |
''
|
The format. |
|
Returns |
string |
Example |
echo Num::format('1234567890', '(000) 000-0000'); // (123) 456-7890
echo Num::format('1234567890', '000.000.0000'); // 123.456.7890
echo Num::format('1234567890', '000.000.0000'); // 123.456.7890
echo Num::format('1', '000.000.000'); // 1..
|
mask_string($string = '', $format = '', $ignore = ' ')
The mask_string method converts a number to a given format.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$string |
string |
''
|
The number to format. |
$format |
string |
''
|
The format. |
$ignore |
string |
''
|
Characters to ignore. |
|
Returns |
string |
Example |
echo Num::mask_string('1234567812345678', '************0000');
// ************5678
echo Num::mask_string('1234567812345678', '**** **** **** 0000');
// **** **** **** 5678
echo Num::mask_string('1234567812345678', '**** - **** - **** - 0000', ' -');
// **** - **** - **** - 5678
|
The format_phone method converts a number to a phone number format.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$string |
string |
''
|
The number to format. |
$format |
string |
null
|
The format. When null it defaults to the format_phone config setting. |
|
Returns |
string |
Example |
echo Num::format_phone('0612345678');
// (061) 234 5678
echo Num::format_phone('0612345678', '(00) 000 000 00');
// (06) 123 456 78
|
The smart_format_phone method converts a number
to a phone number format based on the length of the number.
Note: the numbers are formatted based on length,
these are defined in the num config.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$string |
string |
required |
The number to format. |
|
Returns |
string |
Example |
echo Num::smart_format_phone('1234567');
// 123-4567
echo Num::smart_format_phone('01234567890');
// 0 (123) 456-7890
|
The format_exp method converts a number to a credit card expiration string.
Note: this method expects 4 digits to be supplied.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$string |
string |
required |
The number to format. |
$format |
string |
null
|
The format. When null it defaults to the exp config setting. |
|
Returns |
string |
Example |
echo Num::format_exp('1234');
// 12-34
echo Num::format_exp('1234', '00/00');
// 12/34
|
mask_credit_card($string, $format = null)
The mask_credit_card method masks a credit card number.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$string |
string |
''
|
The number to format. |
$format |
string |
null
|
The format. When null it defaults to the credit_card config setting. |
|
Returns |
string |
Example |
echo Num::mask_credit_card('1234263583742938');
// **** **** **** 2938
echo Num::mask_credit_card('1234123412341234', '0000 **** **** ****');
// 1234 **** **** ****
|