Last active
August 29, 2018 07:45
-
-
Save Caffe1neAdd1ct/96573417a3a9fc5be28df3de63a64d10 to your computer and use it in GitHub Desktop.
Parse a human readable size back to bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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