I suspect you could address security concerns by limiting the input to
valid characters in your arithmetic needs:

if (preg_match('|^[0-9.+/*-]+$|', $expression)){
  eval("$foo = $expression");
  return $foo;
}

On Fri, December 7, 2007 8:51 am, Nathan Rixham wrote:
> In-Built PHP Functions for parsing of basic arithmetic and if possible
> fraction to decimal and decimal to fraction
>
> $arithmetic_string = "3*5";
> echo arith($arithmetic_string); // returns float 15
>
> $arithmetic_string = "1/2";
> echo arith($arithmetic_string); // returns float 0.5
>
> $fraction_string = "1/4";
> echo fracdec($fraction_string); // returns float 0.25
>
> $dec_string = "0.5";
> echo decfrac($dec_string); // returns string "1/4"
>
> I've used php for years, came accross the need for this today and was
> suprised such a basic command wasn't there.. currently the only way I
> can see to do it would be to eval.. which isn't the most secure method
> for such a basic need.
>
> Regards
>
> Nathan
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to