What is the purpose of using regex in PHP for handling floating-point numbers?

When handling floating-point numbers in PHP, it is important to ensure that the input is valid and correctly formatted. Regular expressions (regex) can be used to validate and sanitize user input to ensure it meets the expected format for floating-point numbers. This can help prevent errors and unexpected behavior in calculations or data processing.

// Validate a floating-point number using regex
$floatNumber = "3.14";

if (preg_match('/^-?\d*\.?\d+$/', $floatNumber)) {
    echo "Valid floating-point number: $floatNumber";
} else {
    echo "Invalid floating-point number";
}