MyBB Community Forums

Full Version: Shorten MyBB big numbers to Facebook-like letters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

@"andrewjs18" asked me: "is it possible to shorten threads and posts counters to something more readable by displaying a letter for big numbers, much like Facebook and other companies do?". Yes. Thanks to MyBB's customizability, it is possible. Unfortunately there are not hooks available to easily tweak in this little but handy functionality, so we will use Patches to sneak in our code. Before diving into the tutorial, make sure you have downloaded and installed Patches.

MyBB 1.6 and 1.8 are supported, although the search patterns for every patch has been taken from MyBB 1.8.12. You may need to use different search patterns for older versions of MyBB.

This tutorial will apply to every number displayed within the MyBB environment, it is not limited to post and thread counters.

The final result:

[Image: 1iP4Vth.png]

1. Add the converter function
Within your ACP, go to Plugins > Patches, add, save and apply a new patch with the following configuration:

File: inc/functions.php
Title: my_number_shorten
Search pattern
function my_number_format($number)

Insert before
// Shortens a number and attaches K, M, B, etc. accordingly @https://stackoverflow.com/questions/4371059/shorten-long-numbers-to-k-m-b
function my_number_shorten($number, $precision = 1, $divisors = null) {

    // Setup default $divisors if not provided
    if (!isset($divisors)) {
        $divisors = array(
            pow(1000, 1) => 'k', // Thousand
            pow(1000, 2) => 'm', // Million
            pow(1000, 3) => 'b', // Billion
            pow(1000, 4) => 't', // Trillion
        );    
    }

    // 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 0 + number_format($number / $divisor, $precision) . $shorthand;
}

2. Modify my_number_format
Time to alter the actual MyBB's custom number formatting function. Add, save and apply 2 new patches with the following configurations:

File: inc/functions.php
Title: my_number_format's edit 1
Search pattern
return number_format((double)$number, $decimals, $mybb->settings['decpoint'], $mybb->settings['thousandssep']);

Insert before
if ($number > 999) {
    return my_number_shorten($number);
}

File: inc/functions.php
Title: my_number_format's edit 2
Search pattern
return number_format($number, 0, $mybb->settings['decpoint'], $mybb->settings['thousandssep']);

Insert before
if ($number > 999) {
    return my_number_shorten($number);
}

3. Enjoy!
All big numbers will be now converted to the corresponding shorthand version. You can edit the letter attached to thousands, millions, billions or trillions in the my_number_shorten() function. The crucial part is:

$divisors = array(
    pow(1000, 1) => 'k', // Thousand
    pow(1000, 2) => 'm', // Million
    pow(1000, 3) => 'b', // Billion
    pow(1000, 4) => 't', // Trillion
);

By default, lowercase letters are used but you can use whatever you want.
Very interesting, thanks Shade for such a nice tutorial.
thanks, Shade!
Thanks shade! look like i am gonna use that on my new upcoming forum.
Great, thank you!

Is possible to get the exact number on mouse hover in "title" tag?
Thanks for this! I waited this a long time.

And I have the same question that Eldenroot XDD.
The my_number_format function does not output HTML but just the formatted number, which is then handled in different ways by the functions which uses it. You can wrap the output in such a way that the title tag of an HTML element contains the actual number, but you might end up with issues where the my_number_format output is added, for example, to an HTML attribute of another element.
Ok then, thanks for your reply.

Other question, how can I use this converter with numbers of Stats?

{$stats['numposts']}

{$stats['numthreads']}

{$stats['numusers']}

Thanks again Smile.
This tutorial covers up every number formatted using my_number_format(), and the one you listed are formatted too.
[Image: YXSkuEN.png]

kindly let me know how may i solve this issue ?
Pages: 1 2