Num クラス

The Num class provides additional formatting methods for working with numeric values

設定

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.

Key 規定値 説明
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
パラメータ
パラメータ 規定値 説明
$size string|int|float
0
The string to convert.
返り値 float
echo Num::bytes('200K');  // 204800
echo Num::bytes('5MiB');  // 5242880
echo Num::bytes('1000');  // 1000
echo Num::bytes('2.5GB'); // 2684354560

format_bytes($bytes = 0, $decimals = 0)

The format_bytes method converts a number of bytes to a human readable format.

Static Yes
パラメータ
パラメータ 規定値 説明
$bytes int
0
The number to format.
$decimals int
0
The number of decimals to round up to.
返り値 string|false
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
パラメータ
パラメータ 規定値 説明
$num int 必須 The number to format.
$decimals int
0
The number of decimals to round up to.
返り値 string|false
echo Num::quantity(7000);        // 7K
echo Num::quantity(7500);        // 8K
echo Num::quantity(7500, 1);     // 7.5K
echo Num::quantity('badnumber'); // false

format($string = '', $format = '')

The format method converts a number to a given format.

Static Yes
パラメータ
パラメータ 規定値 説明
$string string
''
The number to format.
$format string
''
The format.
返り値 string
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
パラメータ
パラメータ 規定値 説明
$string string
''
The number to format.
$format string
''
The format.
$ignore string
''
Characters to ignore.
返り値 string
echo Num::mask_string('1234567812345678', '************0000');
// ************5678

echo Num::mask_string('1234567812345678', '**** **** **** 0000');
// **** **** **** 5678

echo Num::mask_string('1234567812345678', '**** - **** - **** - 0000', ' -');
// **** - **** - **** - 5678

format_phone($string = '', $format = null)

The format_phone method converts a number to a phone number format.

Static Yes
パラメータ
パラメータ 規定値 説明
$string string
''
The number to format.
$format string
null
The format. When null it defaults to the format_phone config setting.
返り値 string
echo Num::format_phone('0612345678');
// (061) 234 5678

echo Num::format_phone('0612345678', '(00) 000 000 00');
// (06) 123 456 78

smart_format_phone($string)

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
パラメータ
パラメータ 規定値 説明
$string string 必須 The number to format.
返り値 string
echo Num::smart_format_phone('1234567');
// 123-4567

echo Num::smart_format_phone('01234567890');
// 0 (123) 456-7890

format_exp($string, $format = null)

The format_exp method converts a number to a credit card expiration string.

Note: this method expects 4 digits to be supplied.

Static Yes
パラメータ
パラメータ 規定値 説明
$string string 必須 The number to format.
$format string
null
The format. When null it defaults to the exp config setting.
返り値 string
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
パラメータ
パラメータ 規定値 説明
$string string
''
The number to format.
$format string
null
The format. When null it defaults to the credit_card config setting.
返り値 string
echo Num::mask_credit_card('1234263583742938');
// **** **** **** 2938

echo Num::mask_credit_card('1234123412341234', '0000 **** **** ****');
// 1234 **** **** ****