Number.php
public static function convertToBytes(string $size = null) : float
    {
        // Prepare the size
        $size = trim((string) $size);

        // Construct an OR list of byte units for the regex
        $accepted = implode('|', array_keys(Number::$byte_units));

        // Construct the regex pattern for verifying the size format
        $pattern = '/^([0-9]+(?:\.[0-9]+)?)('.$accepted.')?$/Di';

        // Verify the size format and store the matching parts
        if (!preg_match($pattern, $size, $matches)) {
            throw new \RuntimeException('The byte unit size, "'.$size.'", is improperly formatted.');
        }

        // Find the float value of the size
        $size = (float) $matches[1];

        // Find the actual unit, assume B if no unit specified
        $unit = Arr::get($matches, 2, 'B');

        // Convert the size into bytes
        $bytes = $size * pow(2, Number::$byte_units[$unit]);

        return $bytes;
    }
public static function convertToBytes(string $size = null) : float
    {
        // Prepare the size
        $size = trim((string) $size);

        // Construct an OR list of byte units for the regex
        $accepted = implode('|', array_keys(Number::$byte_units));

        // Construct the regex pattern for verifying the size format
        $pattern = '/^([0-9]+(?:\.[0-9]+)?)('.$accepted.')?$/Di';

        // Verify the size format and store the matching parts
        if (!preg_match($pattern, $size, $matches)) {
            throw new \RuntimeException('The byte unit size, "'.$size.'", is improperly formatted.');
        }

        // Find the float value of the size
        $size = (float) $matches[1];

        // Find the actual unit, assume B if no unit specified
        $unit = Arr::get($matches, 2, 'B');

        // Convert the size into bytes
        $bytes = $size * pow(2, Number::$byte_units[$unit]);

        return $bytes;
    }
public static function convertToBytes ( string $ size = null ) : float { // Prepare the size $ size = trim ( ( string ) $ size ) ; // Construct an OR list of byte units for the regex $ accepted = implode ( '|' , array_keys ( Number :: $ byte_units ) ) ; // Construct the regex pattern for verifying the size format $ pattern = '/^([0-9]+(?:\.[0-9]+)?)(' . $ accepted . ')?$/Di' ; // Verify the size format and store the matching parts if ( ! preg_match ( $ pattern , $ size , $ matches ) ) { throw new \ RuntimeException ( 'The byte unit size, "' . $ size . '", is improperly formatted.' ) ; } // Find the float value of the size $ size = ( float ) $ matches [ 1 ] ; // Find the actual unit, assume B if no unit specified $ unit = Arr :: get ( $ matches , 2 , 'B' ) ; // Convert the size into bytes $ bytes = $ size * pow ( 2 , Number :: $ byte_units [ $ unit ] ) ; return $ bytes ; }
Converts a file size number to a byte value.

echo Number::convertToBytes('200K');  // 204800
echo Number::convertToBytes('5MiB');  // 5242880
echo Number::convertToBytes('2.5GB'); // 2684354560

@param  string $size file size in SB format
@return float
Converts a file size number to a byte value .