Calculator 1.0
Source code
<?php
function calc($foo)
{
$foo = str_replace(",", ".", $foo);
$foo = str_replace(" ", "", $foo);
if ( !preg_match("/^[0-9\*\+\-\/\.\(\)]+$/", $foo) )
{
return "Calculation error - \"$foo\" does not match \"/^[0-9\*\+\-\/\.\(\)]+$/\"";
}
else
{
eval("\$res = $foo;");
return $res;
}
}
/********************/
error_reporting(0);
session_start();
if($_POST['calc_this'])
{
$_SESSION['res'] = calc($_POST['calc_this']);
$_SESSION['this'] = $_POST['calc_this'];
header("location: ?calc_done");
}
?>
<style type="text/css">
.box {
background-color: #fafafa;
padding: 5px;
width: 600px;
border: 1px solid #333;
-moz-border-radius: 10px;
}
.box h1 {
background-color: #555;
margin: -5px;
margin-bottom: 5px;
padding: 5px;
color: #fff;
font-size: 20px;
-moz-border-radius: 10px 10px 0 0;
}
</style>
<div class="box">
<h1>Calculator 1.0</h1>
<form method="post" action="">
<input type="text" name="calc_this" value="<?php echo $_SESSION['this']; ?>" size="50">
<input type="submit" value="=" style="width: 50px;">
<?php echo $_SESSION['res']; ?>
</form>
</div>
<h1>Source code</h1>
<?php highlight_file(__FILE__); ?>