Does the mathematical sum get a text variable? (E.g. 5865/100)

I have a variable that ...

$whatever = "5865/100";

This is a text variable.

I want him to calculate 5865/100, so that I can add it to other numbers and do the calculation.

Number_format does not work, since it just returns "5.865". While I want him to return 58.65

I could do ...

$explode=explode("/",$whatever);
if(count($explode)=="2") {
    $whatever = $explode[0]/$explode[1];
}

But it seems pretty dirty. Is there an easier way?

+5
source share
2 answers

Evaluate the PHP expression, but first check to see if it contains only numbers and operators and space, and suppress any errors.

if (preg_match('/^[\d\+\-\/\*\s]+$/', $s)) {
  @eval('$result = ' . $s . ';');
}
+7
source

eval . , , , - , . , ,

$answer = 0;
$whatever = "5865/100";

eval ('$answer = ' . $whatever . ';');
print($answer);
+2

All Articles