Created
September 9, 2012 20:48
-
-
Save stefanocudini/3687184 to your computer and use it in GitHub Desktop.
convert human to bytes file size
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
| <? | |
| 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