What are the advantages of using filter_var with FILTER_SANITIZE_NUMBER_FLOAT over regex for handling numeric values in PHP?

When handling numeric values in PHP, using filter_var with FILTER_SANITIZE_NUMBER_FLOAT is advantageous over regex because it provides a built-in and secure way to sanitize and validate numeric input. Filter_var ensures that the input is a valid float number, removing any unwanted characters and preventing potential security vulnerabilities. This approach is more reliable and easier to maintain compared to using regex for numeric validation.

// Using filter_var with FILTER_SANITIZE_NUMBER_FLOAT to handle numeric values
$input = "123.45abc";
$filtered_number = filter_var($input, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
if ($filtered_number !== false) {
    echo "Filtered number: " . $filtered_number;
} else {
    echo "Invalid input";
}