Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created September 9, 2012 20:48
Show Gist options
  • Select an option

  • Save stefanocudini/3687184 to your computer and use it in GitHub Desktop.

Select an option

Save stefanocudini/3687184 to your computer and use it in GitHub Desktop.
convert human to bytes file size
<?
function human2bytes_2($t)
{
if( preg_match("#([0-9.]{1,})(B|KB|MB|GB|TB)#",$t,$maches) )
{
$num = $maches[1];
$unit = $maches[2];
$mm = array('B'=>1,
'KB'=>1024,
'MB'=>1024*1024,
'GB'=>1024*1024*1024,
'TB'=>1024*1024*1024*1024);
return round( floatval($num) * $mm[$unit] );
}
else
return false;
}
function human2bytes($t)
{
function tobytes($maches)
{
$num = $maches[1];
$unit = $maches[2];
$mm = array('B'=>1,
'KB'=>1024,
'MB'=>1024*1024,
'GB'=>1024*1024*1024,
'TB'=>1024*1024*1024*1024);
return round( floatval($num)*$mm[$unit] );
}
return preg_replace_callback("#([0-9.]{1,})(B|KB|MB|GB|TB)#",'tobytes',$t);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment