Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shorten MyBB big numbers to Facebook-like letters
#1

@"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.
[Image: fSGNVQj.png]
Reply
#2
Very interesting, thanks Shade for such a nice tutorial.
Reply
#3
thanks, Shade!
Reply
#4
Thanks shade! look like i am gonna use that on my new upcoming forum.
[Image: cafaf18ba2.gif]
FileSquid is under development
[Image: trk1]
Reply
#5
Great, thank you!

Is possible to get the exact number on mouse hover in "title" tag?
[MyBB 1.8 Czech translation] [MyBB 1.8 plugins]: Prune old PMs + optimize DB plugin --- Thank you/like system
Reply
#6
Thanks for this! I waited this a long time.

And I have the same question that Eldenroot XDD.
Reply
#7
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.
[Image: fSGNVQj.png]
Reply
#8
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.
Reply
#9
This tutorial covers up every number formatted using my_number_format(), and the one you listed are formatted too.
[Image: fSGNVQj.png]
Reply
#10
[Image: YXSkuEN.png]

kindly let me know how may i solve this issue ?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)