diff --git a/app/Helpers.php b/app/Helpers.php index cf28f20e..b11f4242 100644 --- a/app/Helpers.php +++ b/app/Helpers.php @@ -158,3 +158,36 @@ function slug_trans($word) { return Phphub\Handler\SlugTranslate::translate($word); } + +// Shortens a number and attaches K, M, B, etc. accordingly +function number_shorten($number, $precision = 1, $divisors = null) { + + if ($number < 1000) { + return $number; + } + // Setup default $divisors if not provided + if (!isset($divisors)) { + $divisors = array( + pow(1000, 0) => '', // 1000^0 == 1 + pow(1000, 1) => 'k', // Thousand + pow(1000, 2) => 'm', // Million + pow(1000, 3) => 'b', // Billion + pow(1000, 4) => 't', // Trillion + pow(1000, 5) => 'Qa', // Quadrillion + pow(1000, 6) => 'Qi', // Quintillion + ); + } + + // Loop through each $divisor and find the + // lowest amount that matches + foreach ($divisors as $divisor => $shorthand) { + if (abs($number) < ($divisor * 1000)) { + // We found a match! + break; + } + } + + // We found our match, or there were no matches. + // Either way, use the last defined value for $divisor. + return number_format($number / $divisor, $precision) . $shorthand; +} \ No newline at end of file diff --git a/resources/views/topics/partials/topics.blade.php b/resources/views/topics/partials/topics.blade.php index 5011c5a7..02cbc715 100644 --- a/resources/views/topics/partials/topics.blade.php +++ b/resources/views/topics/partials/topics.blade.php @@ -22,7 +22,7 @@ / - {{ $topic->view_count }} + {{ number_shorten($topic->view_count) }} |