PHP - Need to calculate the string formula to integer value [duplicate] - php

This question already has answers here:
How to evaluate formula passed as string in PHP?
(1 answer)
PHP: Calculate a math function f(x) in a string
(1 answer)
Closed 9 years ago.
I need to convert the formula like string to its calculated value, in PHP.
For eg:
$str = "(20000+(2000*(40/100)))*(10/100)";
$calculated_value = ????; // Calculated value of the above string "$str".
I need to calculate value of the string "$str" and store it in separate string.
Please help me out.
Thanks in advance.

Take a look on this: http://www.phpclasses.org/package/2695-PHP-Safely-evaluate-mathematical-expressions.html
And Process mathematical equations in php

Related

Best way to convert string[which is basically a comma seprated number] to an integer in PHP [duplicate]

This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 2 years ago.
I have a value like 12,25,246
I want to get 1225246 (must be an integer)
How can I do that?
I have searched a lot but there are direct answer like converting string to integer but not like this actually these both are like integer
I have tried php formate_number but it did not worked.
You could use a combination of intval() and str_replace() to do this.
Example:
$value = '12,25,246';
var_dump(intval(str_replace(',','',$value)));
// Yields: "int(1225246)"
Sandbox
$number = (int)str_replace(',', '', '12,25,246');
here is it

Getting the integer from a string in form of a comma separated/decorated number? [duplicate]

This question already has answers here:
php converting formatted int to int
(2 answers)
Closed 6 years ago.
I am parsing a stream of strings like '1,985' and I want to convert them to the respective integer, like 1985 in this case.
Is there any built-in/efficient PHP function which does that?
Use intval and str_replace for getting your desire result. Intval for your integer number and str_replace for remove the comma from string.
$str = '1,985';
var_dump(intval(str_replace(",", "", $str))); //int(1985)

How to do math with a string? [duplicate]

This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 7 years ago.
So lets show an example here shall we?
Let's say I have 2 strings that are like this....
$math1 = "+300";
$math2 = "-125";
So obviously the answer would have to be +175 but how do I actually do the math while the input is a string.
I can't simply do
$math1 - $math2
Because it'd have to figure out if it is a + or a -, so how can this exactly be done?
You can use intval to get the integer value of a variable, and then you can do basic math operations.
<?php
$math1 = intval("+300");
$math2 = intval("-125");
echo $math1 - $math2;
?>
intval($math1) + intval($math2);
Can be:
$result = abs($math1) - abs($math2);

PHP - How can I determine the numeric sequential value of a character in the english alphabet? [duplicate]

This question already has answers here:
How to convert some character into numeric in php?
(6 answers)
Closed 9 years ago.
Without using a loop, is there a way (function) to convert, for example, the letter "c" to its numeric sequence in the alphabet (3)?
I'm trying to take a literal string - $var = "c" and apply a function to it that returns 3.
Are there any built-in PHP functions that do this? I can't find any online and would rather avoid writing the function if necessary.
Anyone know of such a conversion function?
Yup.
$alphabet_position = ord(strtoupper($character)) - ord('A');

PHP Calculate Math Function by Variable [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP: Calculate a math function f(x) in a string
please anyone help me to solve a math function,
for example i want to solve 3x+2y+4z
the value of the variable should be declared in php,
like $x=4, $y=2, $z=1
i know that there is PHP: Calculate a math function f(x) in a string ,
but is there any simple method ?
You can just create a simple equation:
$val = (3*$x)+(2*$y)+(4*$z)

Categories