2011-03-31 90 views
2

如何將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 

我該如何編碼?請幫忙謝謝。

回答

2

可以使用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 
2

你可以使用printf

printf("%08d", $user_id); 
+0

非常感謝:) – Jorge 2011-03-31 05:31:05

0

$ USER_ID = str_pad($ USER_ID,8, 「0」,STR_PAD_LEFT);

2

PHP有sprintf

$user_str = sprintf("%08d", $user_id); 
echo $user_str; 
2

你可以做str_pad

echo str_pad($user_id,8,'0',STR_PAD_LEFT); 
0
function leadingZeroes($number, $paddingPlaces = 3) { 
    return sprintf('%0' . $paddingPlaces . 'd', $number); 
} 

Source