method saySize documentation in bbn\Str
Converts the bytes to another unit form.
Example
var_dump(\bbn\Str::saySize(50000000000, 'G'));
// (string) "46.57 G"
var_dump(\bbn\Str::saySize(1048576, 'M', 0));
// (string) "1 M"
var_dump(\bbn\Str::saySize(1048576, 'T', 6));
// (string) "0.000001 T"
function($bytes, $unit = 'B', $percision = 2)
{
// pretty printer for byte values
$i = 0;
$units = ['B', 'K', 'M', 'G', 'T'];
if (!in_array(($unit = strtoupper($unit)), $units, true)) {
throw new \Exception(X::_('Invalid provided unit'));
}
while (isset($units[$i]) && $unit !== $units[$i]){
$i++;
$bytes /= 1024;
}
$st = $unit === 'B' ? "%d %s" : "%0.{$percision}f %s";
return sprintf($st, $bytes, $units[$i]);
}
Converts the bytes to another unit form. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions