SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to calculate string type calculation

    Hello,
    Say we have a string type variable holding a mathematical operations and I need to calculate this and put the answer in another variable eg.
    PHP Code:
    $calc '2+2*10'
    Is there a some better way to do it without using eval? I got this working with eval(), but I am not sure if I want to use it.

    Also another question, would using eval() be safe enough if I have pre defined users who are the only ones able to input these arithmetics to the script and in addition if I check that only numbers and arithmetic operators like +-/*. is allowed in the input?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by TeNDoLLA View Post
    Is there a some better way to do it without using eval?
    Another way would be to write a parser. Probably a lot more work though
    Also another question, would using eval() be safe enough if I have pre defined users who are the only ones able to input these arithmetics to the script and in addition if I check that only numbers and arithmetic operators like +-/*. is allowed in the input?
    I guess that it would be safe. But let's hear some more opinions.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want it to support parenthesis, as well as multiple operators in the same statement with different precedence, you will need a fair bit of code. I'd be willing to bet you can find existing code for this.

  4. #4
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I was also thinking of writing my own parser as a last resort but that would be definitely a lot more work than handled with eval().

  5. #5
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    If you want it to support parenthesis, as well as multiple operators, you will need a fair bit of code. I'd be willing to bet you can find existing code for this.
    I have tried searching from google with words like "parse mathematical expressions" etc.. without any success so far.

    If you have any examples / tutorials for this I would appreciate it. It is not needed to be absolutely in php either. Some other languages will do fine also which will show some basic guidelines for doing it.

    Also if anyone of you have some experience from this kind of things I would appreciate every tip that you can give. Like eg. what kind of things I need to consider if I write my own parser class etc.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A problem with eval() is you can't directly do things like square root, as the ^ character is a bitwise operator in php, you'd need to use pow(). So you're going to need to parse it at least a bit.

    Sorry I don't have any links to any code for you. I know you could examine the source code of any programming language, but it might be a hairy read.

  7. #7
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah.. well thanks for replies I'll have to see what I can do about it Looks like this subject is not very common in php.

  8. #8
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again,
    I got this script found on internet as a response on another forum. And this does the job same way almost that eval() does. Anyone care to explain me what exactly is happening in this script? How and what exactly does the mathematical operations in this one?

    PHP Code:
    function calc_string$mathString )
    {
            
    $cf_DoCalc create_function("""return (" $mathString ");" );
           
            return 
    $cf_DoCalc();
    };

    echo(
    '<br>Result: ' calc_string('3*4+2-13*(2.5*10*2*2/4)') );
    echo(
    '<br>Result: ' calc_string('pow(10,3)') ); 

  9. #9
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Discussion here from earlier this year:
    http://www.sitepoint.com/forums/showthread.php?t=545041
    - Robert


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •