0

This is kind of a weird thing and I'm not sure how to phrase the question. I can't find anything that doesn't involve using eval() on strings that already contain numbers. My strings may contain numbers, but there will be "variables" in there as well.

And eval() is strictly out of the question.

The purpose of this is to allow a third-party to write equations that will be stored in a database. The equation will be retrieved and should be evaluated based on user submitted input.

So for a very basic example, let's take calculating the area of a rectangle. Simple w*h. When pulled from the database, the equation is a string:

(total_width * total_height)

The user submits their numbers $_POST['total_width'] and $_POST['total_height']. The script goes and grabs that formula from the database and needs to translate it to:

$_POST['total_width'] * $_POST['total_height']

The problem is, of course, that the equation is a string and those aren't variables.

I've looked at strtr, extract, variables as variables and various custom classes. I'm hoping there's some cool feature that I haven't found yet (like that generator/yield thing) and that doesn't require some super-massive function.

I created a dummy string of $str = "($one * $two)", but that just displays "(1 * 2)" (If, you know, I pass 1 and 2 into it.)

Joe
  • 501
  • 3
  • 17
  • Do the equations they enter always follow a set pattern? – Enstage May 19 '17 at 04:36
  • They do not. I could enter, for example: ((width + height) * 0.95) / 2 – Joe May 19 '17 at 04:38
  • Have you tried to convert it to float? Example: `$width = floatval($_POST['total_width'])` – Jared Chu May 19 '17 at 04:38
  • 2
    This perhaps http://stackoverflow.com/questions/5057320/php-function-to-evaluate-string-like-2-1-as-arithmetic-2-1-1 – Enstage May 19 '17 at 04:40
  • @Enstage I saw that, but it seems to work only if the numbers are already in the string? – Joe May 19 '17 at 04:42
  • @admcfadn I can't go that route. The person maintaining this is an end user and can learn to write the equations, but not PHP functions. – Joe May 19 '17 at 04:44
  • If there is a way to do this, I'd love to know it... But it seems like an extremely big security risk. – admcfajn May 19 '17 at 04:47
  • Maybe something like this, with a front-end where the end-user can build things? I haven't been through it, but teachers are using it to put together puzzles and math problems for the kids: https://studio.code.org/ – admcfajn May 19 '17 at 04:53
  • Or just use a spreadsheet? – admcfajn May 19 '17 at 04:54

2 Answers2

0

I'd just keep a list of the equations as functions... Then explode the string and iterate over it to figure out which function to call.

$num1 = 2;
$num2 = 5;

$equation = 'num1 * num2';
$pieces = explode(" ", $text);

# we now have: ['num1','*','num2']
# then you sort through

$thisEQ = null;
foreach($pieces as $piece){
  if($piece==='num1'||$piece==='num1'){ continue; }

  if($piece==='*'){ $thisEQ = 'multiply_2_int'; }
}

function multiply_2_int($n1,$n2){
  return $n1 * $n2;
}

switch ($thisEQ) {
  case 'multiply_2_int':
    multiply_2_int($num1,$num2);
    break;
  # etc..
}
admcfajn
  • 2,013
  • 3
  • 24
  • 32
0

If I'm understanding what you want, you basically want to allow users to input a formula using algebra and then replace them?

Maybe preg_replace is what you're after?

Example:

<?php

  $x = 5;
  $y = 10;

  $formula = '$x * $y';


  $formula = preg_replace("/".preg_quote('$x', '/')."/", $x, $formula);
  $formula = preg_replace("/".preg_quote('$y', '/')."/", $y, $formula);
  echo $formula;

?>

https://eval.in/800808

A. L
  • 11,695
  • 23
  • 85
  • 163
  • But I would need it to actually return 50, not just the equation. – Joe May 19 '17 at 12:24
  • 2
    Then why not combine my answer with Enstage's? – A. L May 19 '17 at 12:44
  • I won't be using the preg_replace version, but combining your answer with Enstage's is ultimately what I wound up doing. Replace the variable in the string with a number and run it through that class. – Joe May 22 '17 at 19:38