Skip to content

Instantly share code, notes, and snippets.

@Caffe1neAdd1ct
Last active August 29, 2018 07:45
Show Gist options
  • Save Caffe1neAdd1ct/96573417a3a9fc5be28df3de63a64d10 to your computer and use it in GitHub Desktop.
Save Caffe1neAdd1ct/96573417a3a9fc5be28df3de63a64d10 to your computer and use it in GitHub Desktop.
Parse a human readable size back to bytes
<?php
function parseSize($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 $bytesSize = $size * pow(1024, stripos('bkmgtpezy', $unit[0]));
}
return round($size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment