3v4l.org

run code in 300+ PHP versions simultaneously
<?php function calculate_string( $mathString ) { $mathString = trim($mathString); // trim white spaces $mathString = preg_replace ('#[^0-9\+-\*\/\(\) ]#', '', $mathString); // remove any non-numbers chars; exception for math operators $compute = create_function("", "return (" . $mathString . ");" ); return 0 + $compute(); } $string = " (1 + 1) * (2 + 2)"; echo calculate_string($string); // outputs 8

preferences:
48.86 ms | 402 KiB | 5 Q