class Bytes (View source)

Provides helper methods for byte conversions.

Constants

KILOBYTE

The number of bytes in a kilobyte.

ALLOWED_SUFFIXES

The allowed suffixes of a bytes string in lowercase.

Methods

static int
toInt(mixed $size) deprecated

Parses a given byte size.

static float
toNumber(int|float|string $size)

Parses a given byte size.

static bool
validate(string $string)

Validate that a string is a representation of a number of bytes.

Details

static int toInt(mixed $size) deprecated

deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use \Drupal\Component\Utility\Bytes::toNumber() instead

Parses a given byte size.

Parameters

mixed $size

An integer or string size expressed as a number of bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8 bytes, 9mbytes).

Return Value

int

An integer representation of the size in bytes.

See also

https://www.drupal.org/node/3162663

static float toNumber(int|float|string $size)

Parses a given byte size.

Parameters

int|float|string $size

An integer, float, or string size expressed as a number of bytes with optional SI or IEC binary unit prefix (e.g. 2, 2.4, 3K, 5MB, 10G, 6GiB, 8 bytes, 9mbytes).

Return Value

float

The floating point value of the size in bytes.

static bool validate(string $string)

Validate that a string is a representation of a number of bytes.

Parameters

string $string

The string to validate.

Return Value

bool

TRUE if the string is valid, FALSE otherwise.