On 5 October 2015 17:42:08, Rowan Collins <rowan.coll...@gmail.com> wrote:
> An interesting idea. Do you have any idea of the values $format and $unit > would accept? Would it handle both decimal-based GB etc and binary-based GiB > etc [1]? > What about other units that can be scaled in the same way, like metres? Would > it make more sense to make it a unit-formatting function or library - and if > so, does it need to be in core, or could it just be a lightweigh Composer > dependency people could grab when they needed it? > [1] https://en.m.wikipedia.org/wiki/Binary_prefix I've implemented this function and two other size-related in php [1], but I think they should be in php core to avoid implementation of them by thousands of people. This draft is only for demonstation purposes. There are presented three functions: a. size_format([$format,] $bytes [, $unit]): string Formats size in appropriate or specified in $unit unit like sprintf() does. For example, default format is "%.3F Ui" which means: - %.3F is a sprintf-macro that will be replaced by size value - U means upper-cased unit (MB, GB and so on) - i means binary prefix So, formatted string will be like "1.457 KiB" b. size_parse($string): integer Parses string that contains size value and unit in integer containing size in bytes. c. size_transform(float $size, $from, $to): float Translates size in one unit to another. You can see examples of usage in test file [2]. They all support size units up to YB and both decimal (GB) and binary (GiB) prefixes and can translate size values between prefixes. [1] https://github.com/wapmorgan/ext-size [2] https://github.com/wapmorgan/ext-size/blob/master/test.php