What are the advantages of using filter_var() over preg_match() for data validation in PHP?

When validating data in PHP, using filter_var() is preferred over preg_match() because filter_var() is specifically designed for data validation and sanitization. It provides a simpler and more readable way to validate common data types such as emails, URLs, and integers. Additionally, filter_var() automatically handles common validation tasks like checking for valid email formats or valid URLs, reducing the need for complex regular expressions.

// Using filter_var() for data validation
$email = "example@example.com";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Valid email address";
} else {
    echo "Invalid email address";
}