-3

I need to check if the strings have simple equations and display the result.

Examples:

$string = 'Hello world'; // not math
$string = '100 + 10?'; // Results should be 110
$string = 'What is 10 + 5'; // Results should be 15
$string = '1*1=?'; // Results should be 1
$string = 'what is one plus one'; // Results should be 2

the string can contain letters, numbers and characters

I have tried

$string = 'What is 10 + 5';    
$test = preg_replace('/[^0-9,\-\+\*\/]/', "", $string); // 10+5

I'm not sure on how to calculate the result and to check whether the string does have math or not

Solved: I used this code

 if(preg_match('/[0-9\-\+\*\/]/', $string)){
        $test = preg_replace('/[^0-9,\-\+\*\/]/', "", $string);
        if(preg_match('/(\d+)(?:\s*)([\+\-\*\/])(?:\s*)(\d+)/', $test, $matches) !== FALSE){
            $operator = $matches[2];
            switch($operator){
                case '+':
                    $p = $matches[1] + $matches[3];
                    break;
                case '-':
                    $p = $matches[1] - $matches[3];
                    break;
                case '*':
                    $p = $matches[1] * $matches[3];
                    break;
                case '/':
                    $p = $matches[1] / $matches[3];
                    break;
            }
            $test = $p;
        }
        $data['response'] = $test;
    }
  • 2
    Welcome to SO. Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) And [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) SO is **not a free coding service** You have to show that you have made some effort to solve your own problem. – RiggsFolly Jan 10 '16 at 11:08
  • What is the exact expected output for each of those strings? – maxhb Jan 10 '16 at 11:10
  • Use preg_match() to find equations and eval() to calculate the results. – maxhb Jan 10 '16 at 11:13
  • I've edit the description of my question. I hope this helps :) – Denzel Soto Jan 10 '16 at 13:44

1 Answers1

0

This will take care of all but the last one of your strings.

    $mixed = "100 + 10?";
    $letters=array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V','W', 'X', 'Y', 'Z','?','=');
    $only_numbers=str_replace($letters, '', $mixed);

    echo "$only_numbers <br>";

    $only_numbers = eval('return '.$only_numbers.';');

    echo "$only_numbers <br>";

Refer to this:calculate math expression from a string using eval

Community
  • 1
  • 1
T.Shah
  • 2,768
  • 1
  • 14
  • 13
  • Hi @T.Shah Thank you for this. The reference you gave me is very useful but I'm still not sure how to check whether the string does have math to calculate or just plain text ex: 'Hello World' – Denzel Soto Jan 10 '16 at 13:32