function parse_size

7.x common.inc parse_size($size)
6.x common.inc parse_size($size)

Parses a given byte count.

Parameters

$size: A 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

An integer representation of the size in bytes.

Related topics

7 calls to parse_size()
color_scheme_form_submit in drupal-7.x/modules/color/color.module
Form submission handler for color_scheme_form().
CommonSizeTestCase::testCommonParseSize in drupal-7.x/modules/simpletest/tests/common.test
Check that parse_size() returns the proper byte sizes.
CommonSizeTestCase::testCommonParseSizeFormatSize in drupal-7.x/modules/simpletest/tests/common.test
Cross-test parse_size() and format_size().
drupal_check_memory_limit in drupal-7.x/includes/bootstrap.inc
Compares the memory required for an operation to the available memory.
file_field_widget_upload_validators in drupal-7.x/modules/file/file.field.inc
Retrieves the upload validators for a file field.

... See full list

File

drupal-7.x/includes/common.inc, line 1842
Common functions that many Drupal modules will need to reference.

Code

function parse_size($size) {
  $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
  $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
  if ($unit) {
    // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
    return round($size * pow(DRUPAL_KILOBYTE, stripos('bkmgtpezy', $unit[0])));
  }
  else {
    return round($size);
  }
}