Newbie - Transforming string to calculation

This is a discussion on Newbie - Transforming string to calculation within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, if I have a string containing a calculation i.e. "5 * 10 - ((5-2)*(10-3))". Is ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-06-2005
Ryan Dahl
 
Posts: n/a
Default Newbie - Transforming string to calculation

Hi,

if I have a string containing a calculation i.e.
"5 * 10 - ((5-2)*(10-3))".

Is there a simple way for this to be interpreted as values and
operators thus providing a result?!?

Thanks in advance
Ryan Dahl
Reply With Quote
  #2 (permalink)  
Old 12-06-2005
Hilarion
 
Posts: n/a
Default Re: Newbie - Transforming string to calculation

> if I have a string containing a calculation i.e.
> "5 * 10 - ((5-2)*(10-3))".
>
> Is there a simple way for this to be interpreted as values and
> operators thus providing a result?!?



You can use "eval" function:

$expr = '5 * 10 - ((5-2)*(10-3))';
eval( '$result = ' . $expr . ';' );
echo $result;

But remember that eval will evaluate any PHP code, so if the
$expr is comming from external user, than this code allows
the user to execute any PHP code on the server. In that
case you should use some VERY GOOD method of expression
validation to make sure it's only a mathematical expression
before you evaluate it, or use some parser for mathematical
expressions instead of the eval.


Hilarion
Reply With Quote
  #3 (permalink)  
Old 12-06-2005
Connector5
 
Posts: n/a
Default Re: Newbie - Transforming string to calculation

Building on Hilarion's response (eval is by far the simplest method) here is
a function that can be used to remove potentially unsavory characters from a
user-supplied formula. This function isn't perfect, meaning errors can
still exist, but you can do your own error catching later:



function is_formula_safe($formula)
{
$valid = explode(' ', chunk_split('0123456789()^.-+*/ ', 1, ' '));

if (str_replace($valid, '', $formula) != $formula)
{
return false;
}
else
{
return true;
}
}



Sample:




if (is_formula_safe($formula))
{
@eval('$result = ' . $formula . ';');
}
else
{
trigger_error("Invalid Formula: $formula", E_USER_WARNING);
$result = 0;
}




"Hilarion" <hilarion@SPAM.op.SMIECI.pl> wrote in message
news:dn48rc$s24$1@news.onet.pl...
> > if I have a string containing a calculation i.e.
> > "5 * 10 - ((5-2)*(10-3))".
> >
> > Is there a simple way for this to be interpreted as values and
> > operators thus providing a result?!?

>
>
> You can use "eval" function:
>
> $expr = '5 * 10 - ((5-2)*(10-3))';
> eval( '$result = ' . $expr . ';' );
> echo $result;
>
> But remember that eval will evaluate any PHP code, so if the
> $expr is comming from external user, than this code allows
> the user to execute any PHP code on the server. In that
> case you should use some VERY GOOD method of expression
> validation to make sure it's only a mathematical expression
> before you evaluate it, or use some parser for mathematical
> expressions instead of the eval.
>
>
> Hilarion



Reply With Quote
  #4 (permalink)  
Old 12-06-2005
Ryan Dahl
 
Posts: n/a
Default Re: Newbie - Transforming string to calculation

On Tue, 6 Dec 2005 07:42:53 -0800, "Connector5"
<junkmilenko@charter.net> wrote:

>Building on Hilarion's response (eval is by far the simplest method) here is
>a function that can be used to remove potentially unsavory characters from a
>user-supplied formula.


[snip..]

>"Hilarion" <hilarion@SPAM.op.SMIECI.pl> wrote in message
>news:dn48rc$s24$1@news.onet.pl...

[snip..]
>> You can use "eval" function:

[snip..]

Thanks to the both of you. This was actually more straight forward
than expected.

Regards
Ryan
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:56 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0