gabrielelana/byte-units
Composer 安装命令:
composer require gabrielelana/byte-units
包简介
Library to parse, format and convert byte units
README 文档
README
This is a utility component for parsing, formatting, converting and manipulating byte units in various formats.
Usage
<?php // Bytes manipulation and formatting with explici precision echo ByteUnits\parse('1.42MB')->add('256B')->format('kB/0000'); // outputs 1420.2560kB // Bytes comparison ByteUnits\parse('1.2GB')->isMoreThan('256MB'); // it's true
Parsing
Right now two unit systems are supported:
- The
Metricsystem that is based on a 1000-byte kilobyte and uses standard SI suffixes (kB,MB,GB,TB,PB, …) - The
Binarysystem that is based on a 1024-byte kilobyte and uses binary suffixes (KiB,MiB,GiB,TiB,PiB, …)
<?php // Explicit system selection echo ByteUnits\Metric::bytes(1000)->format(); // outputs 1.00kB echo ByteUnits\Binary::bytes(1024)->format(); // outputs 1.00KiB // Implicit selection through parsing ByteUnits\parse('1.00kB'); // it's an instance of ByteUnits\Metric // You can also constraint parsing to a specific system ByteUnits\Metric::parse('1.00kB'); // it's an instance of ByteUnits\Metric ByteUnits\Binary::parse('1.00kB'); // throws a ByteUnits\ParseException // For each systems there are static constructors, one for each supported unit echo ByteUnits\Metric::bytes(1000)->format(); // outputs 1.00kB echo ByteUnits\Metric::kilobytes(1)->format(); // outputs 1.00kB echo ByteUnits\Metric::megabytes(1)->format(); // outputs 1.00MB // You can switch between systems echo ByteUnits\Binary::bytes(1024)->asMetric()->format(); // outputs 1.02kB
Formatting
In both systems you can format bytes with an appropriate format string
<?php // By defaults it tries to format bytes in the most readable unit echo ByteUnits\bytes(1322000)->format(); // outputs 1.32MB echo ByteUnits\bytes(132200)->format(); // outputs 132.20kB // You can force the unit using the related suffix echo ByteUnits\bytes(1322000)->format('MB'); // outputs 1.32MB echo ByteUnits\bytes(1322000)->format('kB'); // outputs 1322.00kB echo ByteUnits\bytes(1322000)->format('B'); // outputs 1322000B // You can choose the precision aka the number of digits after the `.` echo ByteUnits\bytes(1322123)->format(6); // outputs 1.322123MB echo ByteUnits\bytes(1322123)->format('/6'); // outputs 1.322123MB echo ByteUnits\bytes(1322123)->format('MB/6'); // outputs 1.322123MB echo ByteUnits\bytes(1322123)->format('MB/000000'); // outputs 1.322123MB echo ByteUnits\bytes(1322123)->format('GB/9'); // outputs 0.001322123GB // You can specify a separator between then number and the units echo ByteUnits\bytes(1322000)->format('MB', ' '); // outputs 1.32 MB echo ByteUnits\bytes(1322000)->format('MB', '/'); // outputs 1.32/MB // If you don't want to format but get the number of bytes // NOTE: The output is a string to ensure that there's no overflow echo ByteUnits\bytes(1322000)->numberOfBytes(); // outputs 1322000
Compare
There are a few methods that could be used to compare bytes in various units and systems
<?php ByteUnits\Metric::kilobytes(1)->isLessThan(ByteUnits\Binary::kilobytes(1)); // it's true ByteUnits\Metric::kilobytes(1)->isEqualTo(ByteUnits\Binary::bytes(1000)); // it's true ByteUnits\Metric::kilobytes(1.3)->isGreaterThan(ByteUnits\Binary::kilobytes(1)); // it's true
Manipulate
Also you can add or remove bytes in various units and systems
<?php echo ByteUnits\Binary::kilobytes(1)->remove(ByteUnits\Metric::kilobytes(1))->format(); // outputs 24B // Arithmetic operations always preserves the receiving unit system echo ByteUnits\Binary::kilobytes(1)->add(ByteUnits\Metric::kilobytes(1))->format(); // outputs 1.98KiB // You cannot have negative bytes ByteUnits\Metric::kilobytes(1)->remove(ByteUnits\Binary::kilobytes(1))->format(); // throws ByteUnits\NegativeBytesException
Auto Boxing
Most of the methods can take integers or strings and box them to appropriate byte units
ByteUnits\Metric::kilobytes(1)->isLessThan('1KiB'); // it's true echo ByteUnits\Binary::kilobytes(1)->remove('1KiB')->format(); // outputs 24B
Installation via Composer
-
Install Composer to your project root:
curl -sS https://getcomposer.org/installer | php -
Add a
composer.jsonfile to your project:{ "require": { "gabrielelana/byte-units": "^0.5" } } -
Run the Composer installer:
php composer.phar install
Self-Promotion
If you like this project, then consider to:
gabrielelana/byte-units 适用场景与选型建议
gabrielelana/byte-units 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.33M 次下载、GitHub Stars 达 168, 最近一次更新时间为 2014 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「convert」 「parse」 「format」 「byte」 「units」 「size」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gabrielelana/byte-units 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gabrielelana/byte-units 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gabrielelana/byte-units 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a simple decimal/roman number converter
yii2 xml request parser
Softonic OpenApi validation formats extension
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Parse promotion arrays from the Wayne State University API
Tools to convert between .NET and PHP date formats
统计信息
- 总下载量: 2.33M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 175
- 点击次数: 19
- 依赖项目数: 23
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-06-25