How do I convert this string to float number? <?php $s="1/2"; echo (0+($s)); //echos 1 echo floatval($s); //echos 1 ?> How do I convert it to 0.5 in PHP? Thanks
I'm sure you can tidy this up a lot, but it works function handleAddition($str){ $nums = []; $bits = explode('+',$str); //print_r($bits); foreach($bits as $bit){ $nums[] = handleSubtraction($bit); } //print_r($nums); $result = $nums[0]; for($i = 1; $i < count($nums); $i++){ $result += $nums[$i]; } return $result; } function handleSubtraction($str){ $nums = []; $bits = explode('-',$str); //print_r($bits); foreach($bits as $bit){ $nums[] = handleMultiplication($bit); } //print_r($nums); $result = $nums[0]; for($i = 1; $i < count($nums); $i++){ $result -= $nums[$i]; } return $result; } function handleMultiplication($str){ $nums = []; $bits = explode('*',$str); //print_r($bits); foreach($bits as $bit){ $nums[] = handleDivision($bit); } //print_r($nums); $result = $nums[0]; for($i = 1; $i < count($nums); $i++){ $result = $result * $nums[$i]; } return $result; } function handleDivision($str){ $nums = []; $bits = explode('/',$str); //print_r($bits); foreach($bits as $bit){ $nums[] = intval($bit); } //print_r($nums); $result = $nums[0]; for($i = 1; $i < count($nums); $i++){ $result = $result / $nums[$i]; } return $result; } $s="1/2"; echo handleAddition("1+2"); echo '<hr>'; echo handleAddition("5-2"); echo '<hr>'; echo handleAddition("3*2"); echo '<hr>'; echo handleAddition("6/2"); PHP:
Thanks @sarahk I was hoping there might be some function like eval etc that could do the trick. Will use your solution. Thanks!
Assuming that with 1/2 both being variables, why not type cast them as floats upstream of that bit of code?
@SpacePhoenix typecasting also did not worked. $s is storing data from form, like someone enters a math formula, and my PHP executes it. I tried: $s="1/2"; echo (float)$s; // still gives 1, not 0.5 ?> This one below is working, but I do not want to use this because its data taken from form... <?php $s="1/2"; $v=''; $s= '$v= ('.$s.');'; eval( $s ); echo $v; //gives 0.5 ?>
@sarahk What will be regex to allow these symbols "()^%+/*.-%d%s" I am not at all good with regex... I want to allow all those symbols inside quote marks including space and numbers, and remove everything else. Thanks
/^[0-9\(\)\^\%\+\/\*\.\-\" ]*$/ Code (markup): Dirty, but should work. Long story short, do the usual regex and escape any special characters that need to be kept.
@BUMROG Thanks, this worked! $s="?php 1/2 echo 'this'; "; $s= preg_replace( '/[^0-9\(\)\^\%\+\/\*\.\- ]*/', '', $s ); echo $s; // gives 1/2 only Thanks!
All of these are dirty... You should look at PEAR's Math_Fraction (https://pear.php.net/package/Math_Fraction)
Assuming one's hosting allows you to install PEAR stuff, which many do not... Which sucks... which is why you don't find PEAR in a lot of mainstream codebases. It also lacks string input, so one would also still have to split it to make the numerator and denominator, since it accepts either float as a single parameter, or numerator/denominators as separate integers. At which point by the time you trim and typecast, and the result desired is float, you might as well just: function strFractionToFloat($str) { [ $numerator, $denominator ] = explode('/', $str, 2); return intval(trim($numerator)) / intval(trim($denominator)); } Code (markup): Which would be a lot easier than: function strFractionToFloat($str) { [ $numerator, $denominator ] = explode('/', $str, 2); $fraction = Math_Fraction(intval(trim($numerator)), intval(trim($denominator)); return $fraction.toFloat(); } Code (markup): This isn't rocket science and certainly doesn't warrant a regex, much less the security risks of an eval. No need to get a bigass class from an optional library involved when all that's needed is a simple divide. **NOTE** I used PHP 7.1+ destructuring. If you "need" earlier PHP replace [ $numerator, $denominator ] with list($numerator, $denominator)