3 namespace Drupal\Component\Utility;
6 * Provides helper methods for byte conversions.
11 * The number of bytes in a kilobyte.
13 * @see http://wikipedia.org/wiki/Kilobyte
15 const KILOBYTE = 1024;
18 * Parses a given byte size.
21 * An integer or string size expressed as a number of bytes with optional SI
22 * or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8 bytes, 9mbytes).
25 * An integer representation of the size in bytes.
27 public static function toInt($size) {
28 // Remove the non-unit characters from the size.
29 $unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
30 // Remove the non-numeric characters from the size.
31 $size = preg_replace('/[^0-9\.]/', '', $size);
33 // Find the position of the unit in the ordered string which is the power
34 // of magnitude to multiply a kilobyte by.
35 return round($size * pow(self::KILOBYTE, stripos('bkmgtpezy', $unit[0])));