What is the role of regular expressions in email validation within PHP scripts, and what could be causing the validation to fail in the provided code snippet?
The issue with the email validation in the provided code snippet could be due to an incorrect regular expression pattern being used. To fix this, ensure that the regular expression pattern matches the correct format for email addresses. Additionally, check if the input data is being properly sanitized before validation.
$email = "test@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Email is valid.";
} else {
echo "Email is invalid.";
}