Last active
October 2, 2017 12:47
-
-
Save Caffe1neAdd1ct/ef52ede78d4ccc915a88bf29e4f95d4d to your computer and use it in GitHub Desktop.
Convert Human Readable Sizes 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