如何將8位數字作爲標準數字。該號碼將從數據庫中獲取到用戶標識。 示例PHP 8位數字問題
user_id = 1 // This should should be echo as 00000001
user_id = 11 // This should should be echo as 00000011
user_id = 111 // This should should be echo as 00000111
我該如何編碼?請幫忙謝謝。
如何將8位數字作爲標準數字。該號碼將從數據庫中獲取到用戶標識。 示例PHP 8位數字問題
user_id = 1 // This should should be echo as 00000001
user_id = 11 // This should should be echo as 00000011
user_id = 111 // This should should be echo as 00000111
我該如何編碼?請幫忙謝謝。
可以使用printf
功能與%08s
作爲格式字符串:如果你想存儲的結果返回的字符串中可以使用sprintf
作爲
printf("%08s",$user_id);
:
$user_id = sprintf("%08s",$user_id);
格式說明%08s
舉報:
s : Interpret the argument as a string
8 : Print the string left justified within 8 alloted places
0 : Fill the unused places with 0
你可以使用printf
:
printf("%08d", $user_id);
$ USER_ID = str_pad($ USER_ID,8, 「0」,STR_PAD_LEFT);
PHP有sprintf
:
$user_str = sprintf("%08d", $user_id);
echo $user_str;
你可以做str_pad
echo str_pad($user_id,8,'0',STR_PAD_LEFT);
function leadingZeroes($number, $paddingPlaces = 3) {
return sprintf('%0' . $paddingPlaces . 'd', $number);
}
非常感謝:) – Jorge 2011-03-31 05:31:05