Skip to content Skip to sidebar Skip to footer

Number Sort - Gold - Silver - Bronze

I got a problem. I want a working Number sort, thats what I got: $mynumber=7915503; echo substr($mynumber, 0, 3).' Gold '. substr($mynumber, 3, 2).' Silver ' . substr($mynumber, 5,

Solution 1:

You could pad the string, e.g. like this:

$mynumber = substr("0000000", strlen($mynumber)) . $mynumber;

Or if it is an integer:

$bronze = $mynumber % 100;
$silver = ($mynumber / 100) % 100;
$gold = $mynumber / 10000;

That said, if you have the possibility to change the format how those numbers are stored I would do so. Why not take an array?

Solution 2:

You can start from the ending, using with substr negatives numbers. "If start is negative, the returned string will start at the start'th character from the end of string."

Post a Comment for "Number Sort - Gold - Silver - Bronze"