parse_size

Definition

parse_size($size)
includes/common.inc, line 1175

Description

Parse a given byte count.

Parameters

$size A size expressed as a number of bytes with optional SI size and unit suffix (e.g. 2, 3K, 5MB, 10G).

Return value

An integer representation of the size.

Related topics

Namesort iconDescription
FormattingFunctions to format numbers, strings, dates, etc.

Code

<?php
function parse_size($size) {
  $suffixes = array(
    '' => 1,
    'k' => 1024,
    'm' => 1048576, // 1024 * 1024
    'g' => 1073741824, // 1024 * 1024 * 1024
  );
  if (preg_match('/([0-9]+)\s*(k|m|g)?(b?(ytes?)?)/i', $size, $match)) {
    return $match[1] * $suffixes[drupal_strtolower($match[2])];
  }
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.