Skip to content

Instantly share code, notes, and snippets.

@jcicero518
Created January 27, 2018 14:10
Show Gist options
  • Save jcicero518/d018dc243d51d019b1e05747ef170584 to your computer and use it in GitHub Desktop.
Save jcicero518/d018dc243d51d019b1e05747ef170584 to your computer and use it in GitHub Desktop.
<?php
**
* Parse file size
*
* @param string $size File size
* @param string $default Default size
* @return string
*/
function ai1wm_parse_size( $size, $default = null ) {
$suffixes = array(
'' => 1,
'k' => 1000,
'm' => 1000000,
'g' => 1000000000,
);
// Parse size format
if ( preg_match( '/([0-9]+)\s*(k|m|g)?(b?(ytes?)?)/i', $size, $match ) ) {
return $match[1] * $suffixes[ strtolower( $match[2] ) ];
}
return $default;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment