2012-07-12 3 views
3

변수 $ output 내부의 숫자를 천 단위 구분 기호로 바꾸는 중입니다. 현재 출력되는 숫자는 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

답변

3

나는 다음과 같은 코드를 테스트 한 그것은 작동합니다

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

확실하지

왜 코드가 작동하지 않습니다. 또한 소수점을 원한다면 두 번째 매개 변수를 0으로 설정하십시오. 아마도 $ 출력의 값은 문자열이고 number_format()을 통해 전달하기 전에 정수로 캐스팅해야합니까?

1

number_format이 맞는 것 같습니다. 호출하기 전에

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

을 시도해보십시오. 값을 디코딩 한 후 문제가있을 수 있습니다.

관련 문제