473,383 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

string to number conversion with mathematical interpretation

hi all,

I wonder if the following is possible in php:
I want php to 'interpret' a string that contains (a simple kind of)
mathematical calculation, like
$var='25*3';
$result= (int) $var;
//according to the php manual, $result contains 25,
//but I want the result at the end to contain: 75
Is there a php function or construct, that does this?
ok, I know I could write a function that parses and splits the string
$var in the relevant parts, then calculating the result and give it
back. In fact, that wouldn't even be complicated.
But shouldn't it be somehow possible in a 'smarter' way?

thank you for any comment...

Sep 9 '05 #1
3 5181
On Unix you can evaluate the equation by spawning an instance of bc.

Sep 9 '05 #2
"tobimarq" <to******@gmail.com> wrote:
hi all,

I wonder if the following is possible in php:
I want php to 'interpret' a string that contains (a simple kind of)
mathematical calculation, like
$var='25*3';
$result= (int) $var;
//according to the php manual, $result contains 25,
//but I want the result at the end to contain: 75
Is there a php function or construct, that does this?
ok, I know I could write a function that parses and splits the string
$var in the relevant parts, then calculating the result and give it
back. In fact, that wouldn't even be complicated.
But shouldn't it be somehow possible in a 'smarter' way?

thank you for any comment...


# $var must be an expression of the form
#
# int OP int OP ... OP int
#
# where int is a natural number (0, 1, 2, ...) and OP is one of the
# aritmethic operators + - * /

$var='25*3';

if( ! ereg('^[0-9]+([-+*/][0-9]+)*$', $var) ){
echo $var, ": invalid expression";
exit(1);
}

$result = eval("return $var;");
if( ! $result ){
echo $var, ": error evaluating the expression";
exit(1);
}

echo "$var = $result";

Ciao,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it

Sep 10 '05 #3
wow!
nice use of eval(), thanks a lot!
Seems to work with non-integer-numbers as well (when changing the
regex-parts [0-9] to [0-9.])

cu,
Tobias

Sep 10 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ken | last post by:
I've been looking for a solution to a string to long conversion problem that I've run into >>> x = 'e10ea210' >>> print x e10ea210 >>> y=long(x) Traceback (most recent call last): File...
14
by: Dave | last post by:
Hello all, Can anybody help with the problem below? I'm trying to define a conversion function that converts objects to function pointers and am getting a compile error on the line indicated...
14
by: junky_fellow | last post by:
Can anybody please explain this: When a value with integer type is converted to another integer type other than _Bool, if the new type is unsigned, the value is converted by repeatedly...
26
by: junky_fellow | last post by:
Consider the following piece of code: char *str = "Hello"; if (str = "Hello") printf("\nstring matches\n"); str is pointer to char and "Hello" is a string literal whose type is "array of...
6
by: Marco Herrn | last post by:
Hi, I need to serialize an object into a string representation to store it into a database. So the SOAPFormatter seems to be the right formatter for this purpose. Now I have the problem that...
23
by: Bonj | last post by:
what is the correct form of string concatenation in VB.NET, + or &? Both seem to work, but which is correct? I know it's + in C# and & in VB6, but which in VB.NET?
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
4
by: Xah Lee | last post by:
i've long time been interested in algorithmic mathematical art. That is, mathematical or algorithmic visual art works that are generated by computer such that the program's source code reflects the...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.