Skip to content

Instantly share code, notes, and snippets.

@thbighead
Last active May 13, 2019 20:25
Show Gist options
  • Save thbighead/77fc1efab03bff2d23af96bfb5b1b589 to your computer and use it in GitHub Desktop.
Save thbighead/77fc1efab03bff2d23af96bfb5b1b589 to your computer and use it in GitHub Desktop.
Function to cast the shorthand memory notation value to bytes
/**
* Converts shorthand memory notation value to bytes
* From http://php.net/manual/en/function.ini-get.php
*
* @param string $val memory size shorthand notation string
* @return int|string
*/
function return_bytes($val)
{
$val = trim($val);
$last = strtolower($val[strlen($val) - 1]);
$val = substr($val, 0, -1);
switch ($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment