2012-07-12 136 views
3

我試圖很簡單地把變量$輸出裏面的數字變成帶有千位分隔符的數字。它當前輸出的數字是49995,但我希望它顯示爲49,995。使用number_format添加千位分隔符

有些麻煩。幫幫我?

function twitter_followers($user = 'mytwitterusername'){ 
    // Build Twitter api url 
    $apiurl = "http://api.twitter.com/1/users/show.json?screen_name={$user}"; 

    //cache request 
    $transient_key = $user . "_twitter_followers"; 

    // If cached (transient) data are used, output an HTML 
    // comment indicating such 
    $cached = get_transient($transient_key); 

    if (false !== $cached) { 
     return $cached; 
    } 

    // Request the API data, using the constructed URL 
    $remote = wp_remote_get(esc_url($apiurl)); 

    // If the API data request results in an error, return 
    // an appropriate comment 
    if (is_wp_error($remote)) { 
     return '<p>Twitter unaviable</p>'; 
    } 

    // If the API returns a server error in response, output 
    // an error message indicating the server response. 
    if ('200' != $remote['response']['code']) { 
     return '<p>Twitter responded with an HTTP status code of '. esc_html($remote['response']['code']) . '</p>'; 
    } 

    // If the API returns a valid response, the data will be 
    // json-encoded; so decode it. 
    $data = json_decode($remote['body']); 

    $output = $data->followers_count; 
    $followers = number_format($output,2,'.',','); 

    set_transient($transient_key, $output, 600); 

    return $followers; 
} 
+0

應該這樣做,'var_dump($ output);'給你什麼? – jeroen 2012-07-12 20:28:10

回答

3

我測試過下面的代碼和它的作品:

$output = 49995; 
$followers = number_format($output , 0 , '.' , ','); 
echo $followers; 

不知道爲什麼你的代碼不能正常工作。另外請確保將第二個參數設置爲0,除非您想要小數點。也許$輸出的值最初是一個字符串,你需要把它作爲一個整數,然後通過number_format()?

1

您的number_format似乎是正確的。在調用它之前嘗試的

$output = intval($data->followers_count); 

,也許有值解碼後的問題。