Shorten MyBB big numbers to Facebook-like letters

21 Oct 2017 Edited
#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(10001) => 'k'// Thousand
            
pow(10002) => 'm'// Million
            
pow(10003) => 'b'// Billion
            
pow(10004) => '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 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($number0$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(10001) => 'k'// Thousand
    
pow(10002) => 'm'// Million
    
pow(10003) => 'b'// Billion
    
pow(10004) => 't'// Trillion
); 
By default, lowercase letters are used but you can use whatever you want.
phpkiller, Theowner, Eldenroot And 1 others like this post
Theowner 16 Dec 2019
#2
Nice work sir
You really post a wonderful tutorial

Is working fine for me, but it is not working for show thread view.

How can I make it work for the showthread, I will be happy if you can help me sir
By the way this is a wonderful tutorial
Shade 16 Dec 2019
#3
As I told you on the community forums already, if variables are not sanitized with my_number_format this won't work. This is the case for {$thread['views']} in showthread. You either have to alter the core and add something like:

$thread['views'] = my_number_format($thread['views']); 
Before the showthread template is eval'd, or you use PHP in templates and format it directly in your showthread template:

<?php echo my_number_format($thread['views']) ?>
Theowner 16 Dec 2019
#4
@Shade
please enlighten me more sir
i can't understand your reply sir
Shade 16 Dec 2019
#5
If you can't understand such simple instructions, desist or invest a couple of hours into learning PHP basis. It doesn't get any more simple than that I'm afraid.
Theowner 16 Dec 2019
#6
Bro this is my forum https://wapforum.com.ng
Am a friendly type sir, and Am From Nigeria.

if you check my forum, you will see that the view is the only problem am having.

the two codes you just post now, have tried it, rather it just stop my forum from showing
Shade 16 Dec 2019
#7
I can't help further because I don't know how to make it simpler.