Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88694 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 26464 invoked from network); 6 Oct 2015 11:42:22 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Oct 2015 11:42:22 -0000 Authentication-Results: pb1.pair.com header.from=someone.wanted.to.be.unknown@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=someone.wanted.to.be.unknown@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.52 as permitted sender) X-PHP-List-Original-Sender: someone.wanted.to.be.unknown@gmail.com X-Host-Fingerprint: 209.85.220.52 mail-pa0-f52.google.com Received: from [209.85.220.52] ([209.85.220.52:34224] helo=mail-pa0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4E/60-19251-C93B3165 for ; Tue, 06 Oct 2015 07:42:20 -0400 Received: by padhy16 with SMTP id hy16so68318375pad.1 for ; Tue, 06 Oct 2015 04:42:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=CRoxOumhXIUZ05IVVVa/IpH+Q09Nl0RFvqGC6mVBKNg=; b=V3eAhPmwhX28FX8yYDXFgUA7JnG/Um4XCPaXUH41+vCtwQHK/E/nF66kFamLK4b462 t3evNKmruE+B263xdg+ng9YPf4NaAQhZvpFs76SdRRBSNFiHue3klMdq7DGjySudM7Ty EXQAbRNafVs0+LTAz7cl4wSgx81plhmB0ZT7FaA/R//zqsauQWDrp8TRnJa0oH0mIyiS NOGJzWUg6Jg++Up8SZ/GbgXw5DNfghrJmgfBZbJPDQGAxF0lgaScaGgqCyyff8y0MjRY BAtH8nytoEztIf5G3FgI/hMtEgtgO6Ojio2DBuA1Ii8XHV1q9iF4GCWzXBtWa3o+3lHq 2O2g== MIME-Version: 1.0 X-Received: by 10.68.98.227 with SMTP id el3mr33627360pbb.20.1444131737183; Tue, 06 Oct 2015 04:42:17 -0700 (PDT) Received: by 10.66.171.176 with HTTP; Tue, 6 Oct 2015 04:42:17 -0700 (PDT) Date: Tue, 6 Oct 2015 15:42:17 +0400 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=047d7b6dc884342cc105216e1f0c Subject: Re: Re: RFC proposal. From: someone.wanted.to.be.unknown@gmail.com (somebody anon) --047d7b6dc884342cc105216e1f0c Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 5 October 2015 17:42:08, Rowan Collins 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 G= iB etc [1]? > What about other units that can be scaled in the same way, like metres? W= ould 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 Co= mposer 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 --047d7b6dc884342cc105216e1f0c--