Unit Converter
(
GmBowen:) Mark & I modified another script so that it could be an action that could do unit conversions. (doesn't do gallons yet....maybe I'll add that later).
Save the following code as
actions/unitconverter.php and call it with {{unitconverter}}.
<! -- based on Convert v
1.
1 copyright by Bill Pellowe who wrote
"You can do what you want with this" @ the download site-->
<!-- Notes on error checking: Distinguishes singular and plural in the first value. Reloads
if no values are given. Tidied up, faster than
1.
0 -->
<!-- Modified from original program to work in Wakka Wiki
(and related wiki
's) by Mark Kasper & G. Michael Bowen for a research program funded by SSHRC. -->
<!-- Changes to original code copyright Kasper & Bowen, 2004...but released to public domain -->
<?php
// array of key (what to convert) and value (description of it)
$convert_array[celfar] = "degrees Celsius to Fahrenheit";
$convert_array[farcel] = "degrees Fahrenheit to Celsius";
$convert_array[cmin] = "centimeters to inches";
$convert_array[incm] = "inches to centimeters";
$convert_array[mefe] = "meters to feet";
$convert_array[feme] = "feet to meters";
$convert_array[kmmi] = "kilometers to miles";
$convert_array[mikm] = "miles to kilometers";
$convert_array[kglb] = "kilograms to pounds";
$convert_array[lbkg] = "pounds to kilograms";
$convert_array[mloz] = "milliliters to ounces";
$convert_array[ozml] = "ounces to milliliters";
$convert_array[rbtb] = "regular beers to tall beers";
$convert_array[tbrb] = "tall beers to regular beers";
$value1 = $_POST[value1];
$convert = $_POST[convert];
$form_block = "
<form method = \"post\" action = \"$PHP_SELF\">
<p>Enter a number: <input type = \"text\" name=\"value1\" size=10></p>
<p>Change that number from...<br>
<select name =\"convert\">";
if(!($convert)) {
$con_sel = "celfar";
}
else{
$con_sel = $convert;
}
// build the menu
foreach($convert_array as $key=>$desc)
{
if($key=="$con_sel") {
$selected = " selected";
}
else{
$selected = "";
}
$form_block .= "<option value=\""
. $key
. "\""
. $selected
. ">$desc</option>";
} // end of for each
$form_block .= "</select></p>
<p><input type=\"submit\" name=\"submit\" value=\"Do the conversion\"></p>
</form>
<p> </p>
<p> <font size=\"-2\" face=\"verdana, sans-serif\">© 2002 by Bill Pellowe</a></font></p>
";
// remove commas and spaces
trim($value1);
$valuetemp = str_replace(",", "", $value1);
$value = str_replace(" ", "", $valuetemp);
if(!(is_numeric($value)))
{
$value="";
$msg_warn = "<h2>Type a number, no letters.</h2>";
}
// get the first character in the value to later check that it's not a letter
$check =
substr($value,
0,
1);
// in case no number is entered, the page refreshes
if ($value ==
"") {
// echo "<p><h1>Conversion Tool<img border=\"0\" src=\"../images/php-small-white.gif\" align=\"texttop\" hspace=\"30\" width=\"88\" height=\"31\" alt=\"PHP\"></h1></p>";
echo $msg_warn;
echo "<ul>This does a variety of conversions, such as centimeters to inches as so on.<br>";
echo "First, put a number in the box, then choose a calculation, then press the button at the bottom.<br>";
echo "Use a period . for decimal points.</ul>";
echo "<hr>";
echo "$form_block";
} else if (($check !=
"0") &&
($check !=
"1") &&
($check !=
"2") &&
($check !=
"3") &&
($check !=
"4") &&
($check !=
"5") &&
($check !=
"6") &&
($check !=
"7") &&
($check !=
"8") &&
($check !=
"9") &&
($check !=
".") &&
($check !=
"-")) {
// oddly the quotes were needed above around 0 etc for absolute values otherwise letters parsed through
echo "<h1>Conversion Tool</h1></p>";
echo "<ul>This does a variety of conversions, such as centimeters to inches as so on.<br>";
echo "First, put a number in the box, then choose a calculation, then press the button at the bottom.<br>";
echo "Use a period . for decimal points. <strong>It can't start with a letter.</strong></ul>";
echo "<hr>";
echo "$form_block";
} else {
{
if ($value ==
1) {
$plural =
"";
} else
$plural =
"s";
}
switch($convert)
{
case "celfar":
$result =
($value*
1.
8) +
32;
$unit1 =
"degree";
$unit1 .=
$plural;
$unit1 .=
" celsius (C)";
$unit2 =
"degrees fahrenheit (F)";
break;
case "farcel":
$result =
($value -
32) *
0.
5556;
$unit1 =
"degree";
$unit1 .=
$plural;
$unit1 .=
" fahrenheit (F)";
$unit2 =
"degrees celsius (C)";
break;
case "cmin":
$result =
$value *
0.
393701;
$unit1 =
"centimeter";
$unit1 .=
$plural;
$unit2 =
"inches";
break;
case "incm":
$result =
$value *
2.
54;
$unit1 =
"inch";
if ($plural ==
"s") {
$plural =
"es";
}
$unit1 .=
$plural;
$unit2 =
"centimeters";
break;
case "mefe":
$result =
$value *
3.
28084;
$unit1 =
"meter";
$unit1 .=
$plural;
$unit2 =
"feet";
break;
case "feme":
$result =
$value *
0.
3048;
if ($plural !=
"s") {
$unit1 =
"foot";
} else {
$unit1 =
"feet";
}
$unit2 =
"meters";
break;
case "kmmi":
$result =
$value *
0.
621371;
$unit1 =
"kilometer";
$unit1 .=
$plural;
$unit1 .=
" (km)";
$unit2 =
"miles (mi)";
break;
case "mikm":
$result =
$value *
1.
60934;
$unit1 =
"mile";
$unit1 .=
$plural;
$unit1 .=
" (mi)";
$unit2 =
"kilometers (km)";
break;
case "kglb":
$result =
$value *
2.
20462;
$unit1 =
"kilogram";
$unit1 .=
$plural;
$unit1 .=
" (kg)";
$unit2 =
"pounds (lb)";
break;
case "lbkg":
$result =
$value *
0.
453592;
$unit1 =
"pound";
$unit1 .=
$plural;
$unit1 .=
" (lb)";
$unit2 =
"kilograms (kg)";
break;
case "mloz":
$result =
$value *
0.
0338146;
$resultUK =
$value *
0.
035195083;
$unit1 =
"milliliter";
$unit1 .=
$plural;
$unit1 .=
" (ml)";
$unit2 =
"U.S. ounces (oz), or ";
$unit2 .=
number_format($resultUK,
"3",
".",
",");
$unit2 .=
" ounces in British measurement. <br><i>(1 US oz is 1.0408 UK oz)</i>";
break;
case "ozml":
$result =
$value *
29.
57303;
$resultUK =
$value *
28.
41306;
$unit1 =
"ounce";
$unit1 .=
$plural;
$unit1 .=
" (oz)";
$unit2 =
"milliliters, but only if you're using US ounces. <br>If you're using British ounces, the answer is ";
$unit2 .=
number_format($resultUK,
"2",
".",
",");
$unit2 .=
" milliliters (because one US oz is 1.0408 UK oz)";
break;
case "rbtb":
$result =
$value *
0.
75;
$unit1 =
"regular-size can";
$unit1 .=
$plural;
$unit1 .=
" (350 ml, or 12 oz) of beer";
$unit2 =
"tall cans (500 ml, or 16 oz) of beer.<br>(It doesn't matter if you have ml or oz beers; the difference in the ratio between ml and oz beers is only 0.000005 cans of beer.)";
break;
case "tbrb":
$result =
$value *
1.
33;
$unit1 =
"tall can";
$unit1 .=
$plural;
$unit1 .=
" (500 ml, or 16 oz) of beer";
$unit2 =
"regular cans (350 ml, or 12 oz) of beer.<br>(It doesn't matter if you have ml or oz beers; the difference in the ratio between ml and oz beers is only 0.000005 cans of beer.)";
break;
}
echo "<h1>Conversion Tool<a href=\"http://www.php.net\">$php_link</a></p></h1>";
echo "<strong>Result of your conversion:</strong></p>";
echo "<ul><strong>$value</strong> $unit1 is equal to <strong>";
// format the result (number value, decimal places, decimal separator, thousands separator)
echo number_format($result,
"2",
".",
",");
echo "</strong> $unit2.</ul>";
echo "<hr>";
echo "<p>You can do another conversion if you'd like:</p>";
echo "$form_block";
}
?>
CategoryUserContributions