Library to parse, format and convert byte units

Related tags

Numbers byte-units
Overview

Byte Units Build Status

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 Metric system that is based on a 1000-byte kilobyte and uses standard SI suffixes (kB, MB, GB, TB, PB, …)
  • The Binary system 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.json file 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:

Comments
  • Release new version every

    Release new version every

    Hi @gabrielelana,

    Can you please release new version on GitHub? You have merged a few pull requests since last release which forces us to use "dev-master" dependency in composer :(

    Thank you!

    opened by northys 3
  • Add typehinting for IDE autocompletion

    Add typehinting for IDE autocompletion

    Adding a few simple phpdoc comment blocks really simplifies use of the library in the modern IDEs, especially as the library uses fluent interfaces. This PR should introduce no breaking changes - it contains only comments.

    opened by SpareParts 2
  • add getter for numberofbytes

    add getter for numberofbytes

    Just a simple getter method to access the numberofbytes without the need to hack the format function. No int-conversion, just access to the string representation of the number.

    opened by inkrement 0
  • fix petabyte-precision

    fix petabyte-precision

    I cloned your repo and I found that unittest was not running properly on my local environment with php 7.4

    After some research I found that the function bcpow has changed in behaviour in php versione 7.3 (see changelog here)

    bcpow() now returns numbers with the requested scale. Formerly, the returned numbers may have omitted trailing decimal zeroes.

    PHP >= 7.3

    var_dump(bcpow(1024,5,10)); 
    //string(27) "1125899906842624.0000000000"
    
    var_dump(1 * bcpow(1024,5,10)); 
    //float(1.1258999068426E+15)
    

    PHP <7.3

    var_dump(bcpow(1024,5,10));
    //string(16) "1125899906842624"
    
    var_dump(1 * bcpow(1024,5,10));
    //int(1125899906842624)
    

    I've tested the fix with php 7.4 and php 8.0

    opened by lucajackal85 0
  • Addition of an 'Ini' system

    Addition of an 'Ini' system

    As a third unit system:

    • The Ini system that is based on a 1024-byte kilobyte and uses case-insensitive PHP INI suffixes (K, M, G)
    // Implicit selection through parsing
    ByteUnits\parse('8K'); // it's an instance of ByteUnits\Ini
    

    It's an uncommon format, but a common problem:

    • http://php.net/manual/en/function.ini-get.php#refsect1-function.ini-get-examples
    • https://stackoverflow.com/questions/13076480/php-get-actual-maximum-upload-size

    Having the solution in a nice, well-tested package such as this, rather than developers copying and pasting the above code and then rewriting tests (or worse, not!) would be useful.

    opened by weshooper 0
  • Unit conversions

    Unit conversions

    Hi

    Thanks for a nice library. I'd like to be able to do something like this:

    Binary::megabytes(15360)->as('GB'); // result = (int)15
    

    If I gave this a go at some point would you accept a PR? Any advice on how you would prefer it to work?

    Thanks, Mike

    opened by mikehayesuk 3
Releases(0.5.0)
Owner
Gabriele Lana
Software craftsman, clean code disciple, Elixir/Erlang, Haskell, Ruby, Rust, JavaScript, quantified self geek, 20 years on the field and still love it
Gabriele Lana
A library for handling physical quantities and the units of measure in which they're represented.

PHP Units of Measure master: Introduction This is a PHP library for representing and converting physical units of measure. The utility of this library

Jonathan Hanson 21 Sep 28, 2022
Library for converting units and sizes in PHP

php-conversion Library for converting units and sizes in PHP. Units supported Acceleration Angle Area Digital information Electric current Frequency F

Christoffer Niska 122 Mar 16, 2021
Convert Integer to English word

Convert Integer to English Words Laravel package for converting numeric value to english words. Installation You can install the package via composer:

null 5 Oct 4, 2021
Mark Rogoyski 2.2k Dec 29, 2022
Tensor is a library and extension that provides objects for scientific computing in PHP.

Tensor is a library and extension that provides objects for scientific computing in PHP. The multithreaded extension is especially suited for computing large sets of numbers. In some cases, the extension is 230X faster than the same operation in PHPland. Tensor is used by libraries such as Rubix ML to build and accelerate machine learning algorithms such as linear regression, dimensionality reduction, and neural networks.

Rubix 157 Jan 3, 2023
PHP version of Google's phone number handling library

libphonenumber for PHP What is it? A PHP library for parsing, formatting, storing and validating international phone numbers. This library is based on

Joshua Gigg 4.2k Jan 7, 2023
A PHP 5.3+ mathematics library, providing functionality for large numbers

Moontoast Math Library Moontoast\Math is useful for working with integers that are larger than (or may become larger than, through mathematical comput

Moontoast 245 Sep 8, 2022
Advanced Mathematics Library for PHP (port of Numbers.js)

Numbers.php Numbers.php - an advanced mathematics toolkit for PHP >= 5.3. It is a port of Numbers.js - same toolkit for JavaScript. There is a version

null 159 Jul 24, 2022
BigNum library for PHP compatible with bn.js

BigNum library for PHP Information This library provides a PHP Big Number API compatible with bn.js and is used in Fast PHP ECC library elliptic-php.

Simplito 16 Nov 13, 2022
Arbitrary-precision arithmetic library for PHP

Arbitrary-precision arithmetic library for PHP

Brick 1.4k Jan 1, 2023
Unit converter and calculator for php

Unit converter and calculator This library uses the awesome lisachenko/z-engine to allow mathematical operations on objects, allowing to do stuff like

Dominik Chrástecký 8 Apr 8, 2022
A class to help convert bytes into other units (kb, mb, etc).

A class to help convert bytes into other units (kb, mb, etc). This package can be used to convert int|float values from bytes to KB, MB and GB as well

Ryan Chandler 12 Mar 15, 2022
Parse and retrieve data from old format Excel XLS files. MS Excel 97 workbooks PHP reader.

SimpleXLS class 0.9.15 Parse and retrieve data from old Excel .XLS files. MS Excel 97-2003 workbooks PHP reader. PHP BIFF reader. No additional extens

Sergey Shuchkin 160 Jan 6, 2023
This library helps PHP users to convert and using Jalali DateTime format

Easy Jalali for PHP V1.0.0 Jalali calendar converter for Persian users in Iran, Afghanistan and other countries that use Jalali calendar. Very thanks

Majid J 3 Mar 20, 2022
Simplexcel.php - Easily read / parse / convert / write between Microsoft Excel XML / CSV / TSV / HTML / JSON / etc spreadsheet tabular file formats

Simple Excel Easily parse / convert / write between Microsoft Excel XML / CSV / TSV / HTML / JSON / etc formats For further deatails see the GitHuib P

Faisal Salman 550 Dec 27, 2022
Library for converting units and sizes in PHP

php-conversion Library for converting units and sizes in PHP. Units supported Acceleration Angle Area Digital information Electric current Frequency F

Christoffer Niska 127 Dec 23, 2022
A library for handling physical quantities and the units of measure in which they're represented.

PHP Units of Measure master: Introduction This is a PHP library for representing and converting physical units of measure. The utility of this library

Jonathan Hanson 21 Sep 28, 2022
Library for converting units and sizes in PHP

php-conversion Library for converting units and sizes in PHP. Units supported Acceleration Angle Area Digital information Electric current Frequency F

Christoffer Niska 122 Mar 16, 2021
Laravel package to convert English numbers to Bangla number or Bangla text, Bangla month name and Bangla Money Format

Number to Bangla Number, Word or Month Name in Laravel | Get Wordpress Plugin Laravel package to convert English numbers to Bangla number or Bangla te

Md. Rakibul Islam 50 Dec 26, 2022