在PHP中将fraction转换为float

How to convert 1/4 to 0.25 in PHP.

In form I insert number as 1/4. In action page I get the post variable as 1/4. How can I convert this to 0.25. I think in action page this is obtained as string. Thats why it displayed as 1/4. But I need 0.25. How can I do this??

main page

<input type="text" name="a" id="a">

action page

$a = $_POST['a'];
echo $a;  //gives 1/4 but need 0.25

Please help..

You can use a function like this:

<?php

    function calculate_string( $mathString )    {
        $mathString = trim($mathString);
        $mathString = str_replace ('[^0-9\+-\*\/\(\) ]', '', $mathString); 

        $compute = create_function("", "return (" . $mathString . ");" );
        return 0 + $compute();
    }

    echo calculate_string("1/4");


?>

Output:

0.25

one possible way is this:

in action page, first explode the inputed value like:

$explode = explode('/', $_POST['a']);

then you just divide them :D

$result = $explode[0] / $explode[1];
echo $result; //echoes 0.25

L.E: the best way of doing this in my opinion would be to use 3 inputs. One with the first number, one with the operation and one with the second number. In this case you can make a real calculator and perform normal operations in action page like this:

in display page:

<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">

in action page:

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: Just for fun, I made a version for your code with only 1 input

//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;

hope this helps :D

Well you can use eval here but be carefull! Directly parsing $_POST stuff to eval makes your script extremely dangerous for your webserver if you don't escape the input properly!

If you're sure you will get something like 1/4 or 1/2 this will do it:

echo eval("return ".$_POST['a'].";");

but as i said. BE CAREFUL WITH THIS if somebody wants something bad he could just enter exec('init 0') into your input and your server will shutdown if your webserver got the permission to do this (this is just one of the countless vulnurabilities to an unsecured eval) so i please you be patient with this.

A second method is splitting your numbers and dividing them. But that will have many format issues for sure.

Greetings