计算器在php中有一个输入

时间:2016-12-17 13:42:12

标签: php

我想在PHP中创建一个带有一个输入字段和一个提交按钮的计算器。

<html>
<head>
    <title>Calculator</title>
</head>
<body>
    <form method="post" action="">
        <input type="text" name="text1">
        <input type="submit" name="btnSubmit">
    </form>
</body>
</html>

假设,如果我想添加,那么我将在单个输入中输入10 + 12,当我点击提交按钮时,它将显示结果。

<?php
if(isset($_POST["btnSubmit"])){
    $a=$_POST["text1"];
    if($a+$a){
        echo $a+$a;
    }elseif($a-$a){
        echo $a-$a;
    }elseif($a*$a){
        echo $a*$a;
    }elseif($a/$a){
        echo $a/$a;
    }else{
        echo "false";
    }
}

?>

我尝试了一段时间但不能制作逻辑。

3 个答案:

答案 0 :(得分:2)

您可以尝试评估用户提供的字符串,如下所示:

<?php

// Initialise $POST['text1'] for testing purpose
$POST['text1'] = '12*-10';

// IMPORTANT ---------------------------------------------------------------
// It could be dangerous to evaluate text input by a user.
// So, we make sure to evaluate input of the form
// "numeric_value operator numeric_value" only.
// IMPORTANT ---------------------------------------------------------------
if (preg_match('%^[\d-+.]*?[-+*/]{1,1}[\d-+.]*$%', $POST['text1'])) {
    // Prepare a PHP statement containing the user's term.
    // I.e. "return a + b;"
    $term = 'return ' . $POST['text1'] . ';';
    // Evaluate the statement now.
    $res = eval($term);
    if ($res!==false) {
        // Evaluation done correctly.
        echo $res;
    } else {
        // Error in PHP statement.
        echo 'Illegal term. Format "numeric_value operator numeric_value"';
    }
} else {
    echo 'Illegal characters in term';
}

?>

注意:

    在这种情况下,
  • eval 是一种优雅方法。
  • 如果您不确定仅评估适当的表达式,那么它也是危险解决方案。否则,用户的输入可能是有害的,因为他/她可以让您的脚本执行任何可能的PHP语句。因此,代码确保只处理预期的输入:
    if(preg_match(...))

这是 preg_match 中的正则表达式

^[\d-+.]*?[-+*/]{1,1}[\d-+.]*$

Assert position at the beginning of the string «^»
Match a single character present in the list below «[\d-+.]*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
   A single digit 0..9 «\d»
   One of the characters “-+.” «-+.»
Match a single character present in the list below «[\d-+*/]{1,1}»
   Exactly 1 times «{1,1}»
   One of the characters “-+*/” «-+*/»
Match a single character present in the list below «[\d-+.]*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   A single digit 0..9 «\d»
   One of the characters “-+.” «-+.»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

答案 1 :(得分:1)

您可以尝试使用eval和Loop数组的方法:

$string = '61+6-55*1+2-14+1';
    $num = preg_split("/[^0-9]+/", $string);
    $op = (array_filter(preg_split("/[0-9]+/", $string)));
    $a = $num[0];
    $res = 0;
    foreach ($op as $key => $val) {
        $b = $num[$key];
        $res = eval("return $a $val $b;");
        $a = $res;
    }
    var_dump($res);

答案 2 :(得分:0)

您应该按照以下方式进行操作,因为在if子句中,您的评估应该一直下降到true或false。但是你在if和elseif子句中的当前计算并没有这样做。

<?php

if(isset($_POST["btnSubmit"])){
    $a = $_POST["text1"];



    // first get the what operator you have
    // extract the first number or the numbers left side of the operator
    // extract the last number or the numbers right side of the operator
    $operator = false;
    if (stripos($a, "+")) {
        $operator = substr($a, stripos($a, "+"), 1);
        $firstNumber = substr($a, 0, stripos($a, "+"));
        $lastNumber = substr($a, stripos($a, "+")+1);
    } elseif (stripos($a, "-")) {
        $operator = substr($a, stripos($a, "-"), 1);
        $firstNumber = substr($a, 0, stripos($a, "-"));
        $lastNumber = substr($a, stripos($a, "-")+1);
    } elseif (stripos($a, "*")) {
        $operator = substr($a, stripos($a, "*"), 1);
        $firstNumber = substr($a, 0, stripos($a, "*"));
        $lastNumber = substr($a, stripos($a, "*")+1);
    } elseif (stripos($a, "/")) {
        $operator = substr($a, stripos($a, "/"), 1);
        $firstNumber = substr($a, 0, stripos($a, "/"));
        $lastNumber = substr($a, stripos($a, "/")+1);
    }

     // make sure that user submited operator is one of the above operators
    if ($operator == false)
        die("Wrong operator");




   if($operator == '+') {
        echo ($firstNumber + $lastNumber);
    } else if ($operator == '-') {
        echo ($firstNumber - $lastNumber);
    } else if ($operator == '*') {
        echo ($firstNumber * $lastNumber);
    } else if ($operator == '/') {
        echo ($firstNumber / $lastNumber);
    } else {
        echo "false";
    }
}

substr function documentation

stripos documentation