alarik149 Posted January 21, 2007 Share Posted January 21, 2007 Hi people.For a month now I am trying to find or make a good PHP expression evaluator.Please,people that know what I am talking about,help me. Quote Link to comment https://forums.phpfreaks.com/topic/35096-pleasei-need-a-php-expression-evaluator/ Share on other sites More sharing options...
redbullmarky Posted January 21, 2007 Share Posted January 21, 2007 you'll need to be a bit more specific... Quote Link to comment https://forums.phpfreaks.com/topic/35096-pleasei-need-a-php-expression-evaluator/#findComment-165631 Share on other sites More sharing options...
alarik149 Posted January 21, 2007 Author Share Posted January 21, 2007 let's say i have a input text that get's a string,something like f=x+2 and i have to calculate for x=2 and return 4.now I want this to work for all kind of inputed functions,not to create every time a function for every combination of variables and functions.I don't know how to explain better but if it's necesary i'll try to do better.this is usually called a php expression evaluator. Quote Link to comment https://forums.phpfreaks.com/topic/35096-pleasei-need-a-php-expression-evaluator/#findComment-165635 Share on other sites More sharing options...
redbullmarky Posted January 21, 2007 Share Posted January 21, 2007 hmmm. assuming there's nothing out there and that you had to write it yourself, i dont think (for fairly simple equations) it'd be too hard to write your own. a process along the lines of:1, using [url=http://www.php.net/preg_replace]preg_replace[/url] to find and replace adjoining number/letter with a * between them (so that, for example, 2x would become 2*x)2, also using preg_replace to replace the letters with their known values (so that, if x is known and is equal to 4, the equation would be 2*4)3, using [url=http://www.php.net/eval]eval[/url] to actually execute the calculation from the resulting stringhave you tried writing your own as of yet? Quote Link to comment https://forums.phpfreaks.com/topic/35096-pleasei-need-a-php-expression-evaluator/#findComment-165639 Share on other sites More sharing options...
alarik149 Posted January 21, 2007 Author Share Posted January 21, 2007 hmm..i know this is a really hard thing..i don't think that works,still i'm gonna wake a look at it.please i'm waiting for more replies.thank you.. Quote Link to comment https://forums.phpfreaks.com/topic/35096-pleasei-need-a-php-expression-evaluator/#findComment-165645 Share on other sites More sharing options...
redbullmarky Posted January 21, 2007 Share Posted January 21, 2007 it [i]would[/i] definitely work. the problem would come in the case of PHP shifting the equation around (like you'd do if you were solving one for real) depending on where the unknown values are.so [b]f = x+2[/b] would be easily enough, but [b]f + x = 5[/b] wouldnt be as straightforward - although again, a few simple-ish steps would sort that out.it really depends on how complex you see these equations getting. Quote Link to comment https://forums.phpfreaks.com/topic/35096-pleasei-need-a-php-expression-evaluator/#findComment-165649 Share on other sites More sharing options...
utexas_pjm Posted January 21, 2007 Share Posted January 21, 2007 I believe that redbullmarky is suggesting an implementation similar to this:[code]<?php $x = 2; $evalString = 'x + 2'; echo evaluate($evalString, $x); // Output: 4 function evaluate($str, $x) { $str = _prep($str); $f = false; eval ('$f = ' . $str . ';'); return $f; } function _prep($str){ $str = str_replace('x', '$x', $str); return $str; }?>[/code]The _prep function could be extended to handle more advanced input i.e., turning $x^2 to pow($x, 2) etc via regex. I also agree that writing a script to solve linear equations will be another can of worms, however the code provided should handle the trivial case.Best,Patrick Quote Link to comment https://forums.phpfreaks.com/topic/35096-pleasei-need-a-php-expression-evaluator/#findComment-165693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.