How can PHP process input fields that contain formulas instead of simple numbers?

When processing input fields that contain formulas instead of simple numbers, PHP can evaluate the formulas using the `eval()` function. This function takes a string as input and executes it as PHP code. However, it is important to validate and sanitize the input to prevent security vulnerabilities, such as code injection attacks.

$input = $_POST['formula']; // Assuming the input field is named 'formula'
$clean_input = preg_replace("/[^0-9+\-*/.]/", "", $input); // Sanitize the input to allow only numbers and basic operators
$result = eval("return $clean_input;"); // Evaluate the sanitized input as a PHP expression
echo "Result: " . $result; // Output the result of the evaluated formula