0

I have a text file (math.txt) in which any kind of arithmetic operation could be written. I have to read the file using PHP and determine the output. I am using the below mentioned code to read the content of the file.

$file = 'math.txt';  // 2+3 is written in math.txt

$open = fopen($file, 'r');

$read = fgets($open);
$close = fclose($open);

Using the above code, i am getting the content. But echoing the content is displaying the original content (i.e 2+3) rather than displaying the output(i.e 5). I am not understanding what should i do in this case.

Any help on this will be appreciated. Thanks in advance.

Debashis
  • 566
  • 2
  • 14
  • 34
  • 1
    Is this homework? It's ok to ask for help with it but best to admit it up-front. – Neil Neyman Aug 11 '13 at 14:17
  • I am aware of this, but i also mentioned that i am clueless about next step. Looked into this site and found no relative post. So, thought of asking this. It can help in future as well. – Debashis Aug 11 '13 at 14:24
  • If you were to show us your full code, we could then assist you better to find a solution. I.e.: `$total=$var1+$var2; echo $total;` – Funk Forty Niner Aug 11 '13 at 14:42

2 Answers2

1

But echoing the content is displaying the original content (i.e 2+3) rather than displaying the output(i.e 5).

This is completely expected behaviour. You read a string from a file. How should PHP know that you want it to calculate the expression?

You have to implement a simple parser (or search one on the Internet) which analyses the expression and caulates the result.

dave1010 provided a very nice function in one of his posts:

function do_maths($expression) {
  eval('$o = ' . preg_replace('/[^0-9\+\-\*\/\(\)\.]/', '', $expression) . ';');
  return $o;
}

echo do_maths('1+1');

But note that this can still halt your script execution if the input contains a syntax error!

Here is a better library which uses a real parser: https://github.com/stuartwakefield/php-math-parser

Community
  • 1
  • 1
ComFreek
  • 29,044
  • 18
  • 104
  • 156
0

read the file parse according to operator

        like     file=2*5;
             $open = fopen($file, 'r');

              $read = fgets($open);


                $key = preg_split("/[*+-\/]+/", $read);

                 $operator= substr($a, strpos($a,$key[1])-1,1);


                 if($operator=='+')
                 {
                 echo $key[0]+ $key[1];
                 }
                 else  if($operator=='-')
                 {
                 echo $key[0]- $key[1];
                 }
                 else  if($operator=='*')
                 {
                 echo $key[0]* $key[1];
                 }
                 else  if($operator=='/')
                 {
                 echo $key[0]/$key[1];
                 }
alok.kumar
  • 380
  • 3
  • 11
  • If the arithmetic expression is (2+3)*3, will it be supported by the above code? – Debashis Aug 11 '13 at 15:42
  • @Debashis no this solution as written does not handle parentheses. Other things to be aware of include whitespace and perhaps even automatically casting strings to a numeric datatype (especially if such strings include parentheses and whitespace). Best practice is to be explicit about what you are telling php to do. – Neil Neyman Aug 11 '13 at 21:57