PHP Unit Converter

JParra July 17'th 2008 03:13:08 pm

Just a little snippet I created a while back and used recently, This function takes any number of bytes and returns it formatted as the "better looking" Unit.

Usage:

  1. string parse_size(number $size[,string $to][, bool $raw]);

Returns a string containing $size represented as $to followed by $to's suffix, if the optional value $raw is set to true returns a number.

Allowed values for $to are:

  • b
  • kb
  • mb
  • gb
  • tb
  • pb

Example:

  1. parse_size(6789785445);
  2. //6.32 GigaBytes
  3.  
  4. parse_size(6789785445, 'mb');
  5. //6,475.24 MegaBytes
  6.  
  7. parse_size(6789785445, 'mb', true);
  8. //6,475.24

A working example can be found here

The code:

  1. //Parsea un numero dado en bytes a cualquier notaci—n
  2. function parse_size($size, $to = false, $raw = false){
  3. $octal = true;
  4.  
  5. $units = array(
  6. 'b' => array(
  7. 'name' => "Byte",
  8. 'depth' => 0
  9. ),
  10. 'kb' => array(
  11. 'name' => "KiloByte",
  12. 'depth' => 1
  13. ),
  14. 'mb' => array(
  15. 'name' => "MegaByte",
  16. 'depth' => 2
  17. ),
  18. 'gb' => array(
  19. 'name' => "GigaByte",
  20. 'depth' => 3
  21. ),
  22. 'tb' => array(
  23. 'name' => "TeraByte",
  24. 'depth' => 4
  25. ),
  26. 'pb' => array(
  27. 'name' => "PetaByte",
  28. 'depth' => 5
  29. )
  30. );
  31. //Utilizamos escala octal (como en el sistema) o decimal (como en los discos duros)???
  32. $escala = ($octal)?1024:1000;
  33.  
  34. if($to != false && !$units[$to]) return false;
  35. if(!$to){
  36. $loop = true;
  37. $to = 'kb';
  38. }
  39. if($size < $escala){
  40. $to = 'b';
  41. $parsed = $size;
  42. $s = ($parsed == 1)?"":"s";
  43. return number_format($parsed, 2)." {$units[$to]['name']}$s";
  44. }
  45. $ss = pow($escala, $units[$to]['depth']);
  46. $parsed = round($size/$ss, 2);
  47. $s = ($parsed == 1)?"":"s";
  48. if($loop){
  49. $keys = array_keys($units);
  50. $i = 2;
  51. while($parsed > 1 && $i < 6){
  52. $previous = array(
  53. 'value' => $parsed,
  54. 'key' => $keys[$i-1]
  55. );
  56. $to = $keys[$i];
  57. $parsed = parse_size($size, $to, true);
  58. $i++;
  59. }
  60. if($parsed < 1){
  61. $parsed = $previous['value'];
  62. $to = $previous['key'];
  63. }
  64. }
  65. if($raw) return $parsed;
  66. return number_format($parsed, 2)." {$units[$to]['name']}$s";
  67. }

Just a friendly reminder. All the code I post in here is licensed under an MIT License unless explicitly stated.

1 Comments Permalink Tags: php, snippet

Parsing the User Agent String in PHP

JParra April 8'th 2008 03:00:45 pm

Recently I've been working on the stats logger of the blog and stumbled upon a typical WWW problem. The lack of standars, or more accurately the lack of browsers following standards, this time with the User Agent String.

I googled a bit and couldn't find the thing I was going after, the closest thing I found was this function on dotvoid.com, which is pretty limited to the goal I'm after.

So I decided to create a new user agent parser from the ground up, the  goal was to extract as much information as possible as simply as possible, the resulting code consist of 2 functions:


  1. function parse_useragent($agent){
  2. $parts = preg_split('/(?(?![^\(]*\))\s(?!\()|x{7})/', $agent);
  3. $data = array();
  4. foreach($parts as $key => $value){
  5. switch($key){
  6. case 0:
  7. list($data['product'], $comment) = explode(' (', $value);
  8.  
  9. $info = analyze_product_comment($comment);
  10. break;
  11. case 1:
  12. $data['engine'] = $value;
  13. break;
  14. case 2:
  15. if(stristr($value, 'version'))
  16. $data['engine'] .= " $value";
  17. else
  18. $data['browser'] = $value;
  19. break;
  20. case 3:
  21. $data['browser'] = $value;
  22. break;
  23. case 4:
  24. $data['browser'] = $value . " " .$parts[3];
  25. break;
  26. }
  27. }
  28. return $data+$info;
  29. }
  30. function analyze_product_comment($value){
  31. $value = str_ireplace(array("(", ")"), "", $value);
  32. $parts = explode("; ", $value);
  33. foreach($parts as $key => $val){
  34. switch($key){
  35. case 0:
  36. if(strtolower($val) == 'compatible'){//Usually IE or bots
  37. $info = array(
  38. 'browser' => $parts[1],//Browser
  39. 'os' => $parts[2]//OS
  40. );
  41. return $info;
  42. }
  43. $info['platform'] = $val;//Platform
  44. break;
  45. case 1:
  46. if(eregi('[uin]', $val))
  47. $info['security'] = $val;//Security
  48. break;
  49. case 2:
  50. $info['os'] = $val;//OS
  51. break;
  52.  
  53. case 3:
  54. if(eregi('[a-z\-]{2,6}', $val))
  55. $info['language'] = $val;//Language
  56. break;
  57. }
  58. }
  59. return $info;
  60. }

And you just use it by calling the parse_useragent function with the user agent as the first parameter.

Example:

  1. $client = parse_useragent($_SERVER['HTTP_USER_AGENT']);
  2. print_r($client);
  3. /* Outputs
  4. array (
  5.   'product' => 'Mozilla/5.0',
  6.   'engine' => 'Gecko/2008032619',
  7.   'browser' => 'Firefox/3.0b5',
  8.   'platform' => 'Macintosh',
  9.   'security' => 'U',
  10.   'os' => 'Intel Mac OS X 10.5',
  11.   'language' => 'en-US',
  12. )
  13. */

Just a friendly reminder. All the code I post in here is licensed under an MIT License unless explicitly stated.

4 Comments Permalink Tags: php, stats, snippet