Home > Micro guides > Concept unit converter

Concept unit converter

February 12th, 2009

Introduction

This is my fourth guide and is, like my prime generator, just some code I did for fun, while I was bored in front of my computer:)
So what are we going to do today? Well, we’re going to program a unit converter “Google-style”. What I wanted to do was to get a user to input a unit. For instance “4 pounds” and then the script would output how much this was in several other units. And because units like “pounds” can be addresses in many ways (like “lb”, “lbs”, “pound”, and so on) I wanted to make the script understand every single of them and use them as if the user had used the input “pounds”.

The code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Concept unit converter
echo "You can use this script to convert from/to any of the most common units in mass, distance and speed<br/>Just enter, for instance: 13 km, and then the system will calculate how long 13 km is in other units.";
echo '<form action="test.php" method="get"><input type="text" name="q" size="15"/><input type="submit" value="submit"/></form>';
$input = strtolower($_GET['q']);
$number = floatval($input);
list( , $unit) = preg_split("/[\s,.1234567890]+/", $input);
$original_unit = $unit;
//Mass
$allowed["mass"] = array("kilogram" => "kg", "kilograms" => "kg", "kgs" => "kg", "grams" => "g", "gram" => "g", "lb" => "lbs", "pounds" => "lbs", "pound" => "lbs", "ounces" => "ounce", "stones" => "stone");
$conv["mass"] = array("kg" => 1, "lbs" => 2.204623, "g" => 1000, "stone" => 0.157473, "ounce" => 35.27);
 
// Distance
$allowed["distance"] = array("meter" => "m", "meters" => "m", "kilometer" => "km", "kilometers" => "km", "inch" => "in", "inches" => "in", "mile" => "mi", "miles" => "mi", "yard" => "yd", "yards" => "yd");
$conv["distance"] = array("m" => 1, "km" => 0.001, "in" => 39.37007874, "mi" => 0.0006213712, "yd" => 1.093613);
 
// Speed / velocity
$allowed["speed"] = array("kmh" => "km/h", "mps" => "m/s", "kms" => "km/s");
$conv["speed"] = array("km/h" => 1, "mph" => 0.6213712, "m/s" => 0.2777778, "km/s" => 0.0002777778);
 
$types = array("mass", "distance", "speed");
 
foreach($types as $type){
	if(array_key_exists($unit,$allowed[$type]) || array_key_exists($unit,$conv[$type])){
		if(array_key_exists($unit,$allowed[$type]))
			$unit = $allowed[$type][$unit];
		echo "<table><tr><td style=\"text-align:right;\">";
		echo "<b>" . number_format($number,2,"."," ") . "</b></td><td><b> " . $original_unit . "</b></td></tr>";
		$number = $number/$conv[$type][$unit];
		foreach($conv[$type] as $unit_conv => $conv_ratio){
			if($unit != $unit_conv)
				echo "<tr><td style=\"text-align:right;\">" . number_format($number*$conv_ratio,2,"."," ") . "</td> <td>$unit_conv</td></tr>";
		}
		echo "</table>";
	}
}

It is clear from the code that the array $allowed[] specifies what terms are allowed in the script and it’s a translation table to the correct unit (”the correct unit” is specified by you). The array $conv[] contains the conversion ratio. As you see from the code the conversion ratio is always relative to the first unit in $conv[].

Good luck converting:D

Important note: This script can only be used for units with a specific ratios between them. This means that it for instance cannot be used for converting Celsius to Fahrenheit and vice versa.

Ricehigh Micro guides , ,

  1. No comments yet.
  1. No trackbacks yet.

Please copy the string kAHCWd to the field below: