Advertisement
austinh115

[PHP] Conversion of units to other units. What a masterpiece

Jul 9th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. <?php
  2.  
  3. $command = explode(" ", "!convert 25 psi bar");
  4.  
  5. if(count($command) < 4 || !is_numeric($command[1])) {
  6.     echo "usage";
  7.     return;
  8. }
  9.  
  10. //MEASUREMENTS =>
  11.  
  12.  
  13. $Pressure = [
  14.     "bar" => [
  15.         "bar" => 1,
  16.         "kg" => 1.02,
  17.         "psi" => 14.5,
  18.     ],
  19.     "kg" => [
  20.         "bar" => 0.98,
  21.         "kg" => 1,
  22.         "psi" => 14.2,
  23.     ],
  24.     "psi" => [
  25.         "bar" => 0.069,
  26.         "kg" => 0.0703,
  27.         "psi" => 1,
  28.     ],
  29. ];
  30.  
  31. if(!(in_array($command[2], array_keys($Pressure)) && in_array($command[3], array_keys($Pressure)))) {
  32.     echo "usage";
  33.     return;
  34. }
  35.  
  36. print "{$command[1]} {$command[2]} = " . ($command[1] * $Pressure[$command[2]][$command[3]]) . " {$command[3]}\n";
  37.  
  38. //
  39.  
  40. $command = explode(" ", "!convert 1 miles millimeters");
  41.  
  42. if(count($command) < 4 || !is_numeric($command[1])) {
  43.     echo "usage";
  44.     return;
  45. }
  46.  
  47. $Length = [
  48.     "millimeters" => 1,
  49.     "centimeters" => .1,
  50.     "miles" => 0.0000006214,
  51.     "inches" => 0.0393701,
  52.     "meters" => 0.001,
  53.     "kilometers" => 0.000001,
  54.     "feet" => 0.00328084,
  55.     "yards" => 0.00109361,
  56. ];
  57.  
  58. if(!(in_array($command[2], array_keys($Length)) && in_array($command[3], array_keys($Length)))) {
  59.     echo "usage\n";
  60.     return;
  61. }
  62.  
  63. print "{$command[1]} {$command[2]} = " . ($Length[$command[3]] / $Length[$command[2]] * $command[1]) . " {$command[3]}\n";
  64.  
  65. //
  66.  
  67. $command = explode(" ", "!convert 1 grams oz");
  68.  
  69. if(count($command) < 4 || !is_numeric($command[1])) {
  70.     echo "usage";
  71.     return;
  72. }
  73.  
  74. $Weight = [
  75.     "grams" => 1,
  76.     "oz" => 0.035274,
  77.     "kilograms" => 0.001,
  78.     "pounds" => 0.00220462,
  79.     "metrictons" => 0.000001,
  80.     "tons" => 0.0000010231,
  81. ];
  82.  
  83. if(!(in_array($command[2], array_keys($Weight)) && in_array($command[3], array_keys($Weight)))) {
  84.     echo "usage\n";
  85.     return;
  86. }
  87.  
  88. print "{$command[1]} {$command[2]} = " . ($Weight[$command[3]] / $Weight[$command[2]] * $command[1]) . " {$command[3]}\n";
  89.  
  90. //
  91.  
  92. $command = explode(" ", "!convert 1 milliliters ounces");
  93.  
  94. if(count($command) < 4 || !is_numeric($command[1])) {
  95.     echo "usage";
  96.     return;
  97. }
  98.  
  99. $Volume = [
  100.     "milliliters" => 1,
  101.     "ounces" => 0.033814,
  102.     "quarts" => 0.00105669,
  103.     "pints" => 0.00211338,
  104.     "cups" => 0.00422675,
  105.     "liters" => 0.001,
  106.     "gallons" => 0.00026417,
  107. ];
  108.  
  109. if(!(in_array($command[2], array_keys($Volume)) && in_array($command[3], array_keys($Volume)))) {
  110.     echo "usage\n";
  111.     return;
  112. }
  113.  
  114. print "{$command[1]} {$command[2]} = " . ($Volume[$command[3]] / $Volume[$command[2]] * $command[1]) . " {$command[3]}\n";
  115.  
  116. // BASE: base_convert
  117.  
  118. $command = explode(" ", "!convert 11111111 bin hdeci");
  119.  
  120. if(count($command) < 4 || !is_numeric($command[1])) {
  121.     echo "usage";
  122.     return;
  123. }
  124.  
  125. $bases = [
  126.     "hdeci" => 16,
  127.     "dec" => 10,
  128.     "oct" => 8,
  129.     "bin" => 2,
  130. ];
  131.  
  132. if(!(in_array($command[2], array_keys($bases)) && in_array($command[3], array_keys($bases)))) {
  133.     echo "usage\n";
  134.     return;
  135. }
  136.  
  137. print "{$command[1]} in {$command[2]} = " . strtoupper(base_convert($command[1], $bases[$command[2]], $bases[$command[3]])) . " in {$command[3]}\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement