0

I have a php script that echoes the view count of a Youtube video using the Youtube API. However, I would like it to add commas when reaching views in the thousands and over.

My code is below:

$JSON = file_get_contents('https://gdata.youtube.com/feeds/api/videos/XQu8TTBmGhA?v=2&alt=json');
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo $views;

Any help would be highly appreciated!

1

3 Answers 3

1

Use number_format()

echo number_format($views);
0
0

Use this

echo  number_format($views, 2, '.', ',');
0

http://php.net/manual/en/function.number-format.php

PHP function number_format

Example:

setlocale(LC_ALL, '');
$locale = localeconv();
$decimals = 2;
echo number_format($views, $decimals, $locale['decimal_point'], $locale['thousands_sep']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.