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
I doubt this is needed in core, but sounds "ok" for an extension.
Also, I believe, quite a decent version of this can be written in php
(think PEAR)
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
--
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
I doubt this is needed in core, but sounds "ok" for an extension.
Yes, I'm sure a simple extension using re2c to parse the expressions
would be gladly accepted into PECL.
I don't see any need for this in the core, though.
Also, I believe, quite a decent version of this can be written in php
(think PEAR)
This could be an option either.
--
Wbr,
Antony Dovgal
Agreed, PECL or PEAR, some provision should be made, it's worth the
extra few bytes of code.
Thanks for the opinions
Nathan
Antony Dovgal wrote:
I doubt this is needed in core, but sounds "ok" for an extension.
Yes, I'm sure a simple extension using re2c to parse the expressions
would be gladly accepted into PECL.
I don't see any need for this in the core, though.Also, I believe, quite a decent version of this can be written in php
(think PEAR)This could be an option either.
Antony Dovgal wrote:
I doubt this is needed in core, but sounds "ok" for an extension.
Yes, I'm sure a simple extension using re2c to parse the expressions
would be gladly accepted into PECL.
or something libeval based ...
http://members.bellatlantic.net/~dutky/libeval.html
i already started a pecl wrapper for that onece
but somehow lost interest. Don't remember why,
it was either license issues (it's LGPL now but
might have been GPL back then) or build system
issues on the library itself ...
i'll give it another look tonight in parallel
to the TV maybe ...
--
Hartmut Holzgraefe, Principal Support Engineer
.
Discover new MySQL Monitoring & Advisory features at:
http://www.mysql.com/products/enterprise/whats_new.html
Hauptsitz: MySQL GmbH, Dachauer Str.37, 80335 München
Geschäftsführer: Kaj Arnö - HRB München 162140
In-Built PHP Functions for parsing of basic arithmetic and if possible
fraction to decimal and decimal to fraction
PHP already handles half of what you're looking for by default.
$arithmetic_string = "3*5";
echo arith($arithmetic_string); // returns float 15
<?=3 * 5;?>
$arithmetic_string = "1/2";
echo arith($arithmetic_string); // returns float 0.5
<?=1 / 2;?>
$fraction_string = "1/4";
echo fracdec($fraction_string); // returns float 0.25
<?=1 / 4; ?>
$dec_string = "0.5";
echo decfrac($dec_string); // returns string "1/4"
Now I see why you need PHP to do your math for you! ;-P
There are functions written that do this now though.
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
--
--
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]
If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
Daniel and Derick, thanks for the replies..
Daniel first.. <?=3 * 5;?> is great :) however
$arithmetic_string = "3*5";
$arithmetic_value = arith($arithmetic_string);
not possible with the method you mentioned.
eval() yes this works, however personally I feel a simple arith() (or
more suitably named function) would be a great deal quicker and safer
than eval($_POST['sum']), the security dangers are somewhat obvious.
ps: I must point out that I've still never had a use for this and never
would (perhaps the fraction??), but somebody over on a dev forum does,
and I was quite suprised this functionality wasn't included! :P
Nathan
Daniel Brown wrote:
In-Built PHP Functions for parsing of basic arithmetic and if possible
fraction to decimal and decimal to fractionPHP already handles half of what you're looking for by default.
$arithmetic_string = "3*5";
echo arith($arithmetic_string); // returns float 15<?=3 * 5;?>
$arithmetic_string = "1/2";
echo arith($arithmetic_string); // returns float 0.5<?=1 / 2;?>
$fraction_string = "1/4";
echo fracdec($fraction_string); // returns float 0.25<?=1 / 4; ?>
$dec_string = "0.5";
echo decfrac($dec_string); // returns string "1/4"Now I see why you need PHP to do your math for you! ;-P There are functions written that do this now though.
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
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
What's wrong with eval?
<?php
eval( '$res = 3 * 5;' );
echo $res, "\n";
regards,
Derick
--
Derick Rethans
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org
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 15What's wrong with eval?
<?php
eval( '$res = 3 * 5;' );
echo $res, "\n";
Maybe it's from user input? And it is generally taught that if eval() is
the solution then you're on the wrong track.
Cheers,
Rob.
...........................................................
SwarmBuy.com - http://www.swarmbuy.com
Leveraging the buying power of the masses!
...........................................................
Yes, an easy way to handle this functionality that is safe to use with
user input would be REALLY nice.
Specifically for allowing users to specify custom formulas to do all
sorts of nifty stuff. Especially in reporting and payroll/commission
applications.
We're currently working on a reporting class where we would like the
user to able to specify custom columns in XML, ie:
$col5 = ($col1 / $col2) * ($col3 / $col4)
Or in our payroll application where customers want to calculate custom
commissions for sales people:
$commission = ($gross_sales - $gross_pay - $medical_benefits -
$chargebacks) * $employee_commission_bracket
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
--
Mike <ipso@snappymail.ca
Yes, an easy way to handle this functionality that is safe to use with
user input would be REALLY nice.Specifically for allowing users to specify custom formulas to do all
sorts of nifty stuff. Especially in reporting and payroll/commission
applications.We're currently working on a reporting class where we would like the
user to able to specify custom columns in XML, ie:$col5 = ($col1 / $col2) * ($col3 / $col4)
Or in our payroll application where customers want to calculate custom
commissions for sales people:$commission = ($gross_sales - $gross_pay - $medical_benefits -
$chargebacks) * $employee_commission_bracket
In this case, you might find embedding usable…
http://pecl.php.net/package/lua
http://pecl.php.net/package/python
http://pecl.php.net/package/perl
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
I've thought about this for the remainder of the day, and I can't see
any good reason why this functionality should not be implemented into
the core; it's basic fucntionality, a safe way of doing things and
couldn't take more than a day or two to add in from a single developer,
and surely it'd be no more than a couple of kb's worth compiled, if that!
Also, it's a bit of minor piece of functionality to warrent a PECL/PEAR
Extension, and even more hassle for somebody to reasearch, find and
download it - all to do some basic arithmetic.
Can anybody give me an argument as to why this functionality should not
be incorporated into the core?
Many Regards
Nathan Rixham
Alexey Zakhlestin wrote:
Yes, an easy way to handle this functionality that is safe to use with
user input would be REALLY nice.Specifically for allowing users to specify custom formulas to do all
sorts of nifty stuff. Especially in reporting and payroll/commission
applications.We're currently working on a reporting class where we would like the
user to able to specify custom columns in XML, ie:$col5 = ($col1 / $col2) * ($col3 / $col4)
Or in our payroll application where customers want to calculate custom
commissions for sales people:$commission = ($gross_sales - $gross_pay - $medical_benefits -
$chargebacks) * $employee_commission_bracketIn this case, you might find embedding usable…
http://pecl.php.net/package/lua
http://pecl.php.net/package/python
http://pecl.php.net/package/perl
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;
}
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
--
--
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?