一種可能的方式是這樣的:
在操作頁面
,第一爆炸在輸入的值一樣:
$explode = explode('/', $_POST['a']);
那麼你只要將它們劃分:d
$result = $explode[0]/$explode[1];
echo $result; //echoes 0.25
L.E:在我看來,這樣做是使用3個輸入的最佳方式。一個帶有第一個數字,一個帶有操作,另一個帶有第二個數字。在這種情況下,你可以做一個真正的計算器和在行動頁面這樣執行正常操作:
在顯示頁面:
<input type="text" name="first_no" id="first_no">
<input type="text" name="operation" id="operation">
<input type="text" name="second_no" id="second_no">
在行動頁:
switch($_POST['operation']) {
case '+';
$result = $_POST['first_no'] + $_POST['second_no'];
break;
case '-';
$result = $_POST['first_no'] - $_POST['second_no'];
break;
case '*';
$result = $_POST['first_no'] * $_POST['second_no'];
break;
case '/';
$result = $_POST['first_no']/$_POST['second_no'];
break;
//and so on... if you need more
}
echo $result;
L.E2:只是爲了好玩,我爲您的代碼提供了一個版本,只有一個輸入
//get index
preg_match("/\D/is", $_POST['a'], $mList, PREG_OFFSET_CAPTURE);
$index = $mList[0][1];
//get operation
$operation = substr($string, $index, 1);
//get numbers
$explode = explode($operation, $string);
//produce result
switch($operation) {
case '+';
$result = $explode[0] + $explode[1];
break;
case '-';
$result = $explode[0] - $explode[1];
break;
case '*';
$result = $explode[0] * $explode[1];
break;
case '/';
$result = $explode[0]/$explode[1];
break;
//and so on... if you need more
}
echo $result;
希望這有助於:d
檢查我的回答,我提供了一些很好的辦法來解決你的這個問題;)取我主張一個特殊的外觀爲您的代碼,並在使用'EVAL的最後一個版本 – 2014-11-24 13:26:20