class Bytes
Provides helper methods for byte conversions.
Hierarchy
- class \Drupal\Component\Utility\Bytes
Expanded class hierarchy of Bytes
11 files declare their use of Bytes
- BytesTest.php in core/tests/ Drupal/ Tests/ Component/ Utility/ BytesTest.php 
- color.module in core/modules/ color/ color.module 
- Allows users to change the color scheme of themes.
- common.inc in core/includes/ common.inc 
- Common functions that many Drupal modules will need to reference.
- EditorImageDialog.php in core/modules/ editor/ src/ Form/ EditorImageDialog.php 
- FileItem.php in core/modules/ file/ src/ Plugin/ Field/ FieldType/ FileItem.php 
2 string references to 'Bytes'
- FieldFileSizeTest::testFieldFileSize in core/modules/ views/ tests/ src/ Kernel/ Handler/ FieldFileSizeTest.php 
- FileSize::render in core/modules/ views/ src/ Plugin/ views/ field/ FileSize.php 
- Renders the field.
File
- 
              core/lib/ Drupal/ Component/ Utility/ Bytes.php, line 8 
Namespace
Drupal\Component\UtilityView source
class Bytes {
  
  /**
   * The number of bytes in a kilobyte.
   *
   * @see http://wikipedia.org/wiki/Kilobyte
   */
  const KILOBYTE = 1024;
  
  /**
   * Parses a given byte size.
   *
   * @param 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 int
   *   An integer representation of the size in bytes.
   */
  public static function toInt($size) {
    // Remove the non-unit characters from the size.
    $unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
    // Remove the non-numeric characters from the size.
    $size = preg_replace('/[^0-9\\.]/', '', $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(self::KILOBYTE, stripos('bkmgtpezy', $unit[0])));
    }
    else {
      // Ensure size is a proper number type.
      return round((double) $size);
    }
  }
}Members
| Title Sort descending | Modifiers | Object type | Summary | 
|---|---|---|---|
| Bytes::KILOBYTE | constant | The number of bytes in a kilobyte. | |
| Bytes::toInt | public static | function | Parses a given byte size. | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.
