filter_var()
function :<?php
$email = "john.doe@example.com";
// Sanitize email address
$sanitized_email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate sanitized email address
if (filter_var($sanitized_email, FILTER_VALIDATE_EMAIL)) {
echo "The email address $sanitized_email is valid.";
} else {
echo "The email address $sanitized_email is not valid.";
}
?>
The email address john.doe@example.com is valid.
FILTER_SANITIZE_EMAIL
filter, which removes all illegal characters from the email address. The sanitized email address is then validated using the FILTER_VALIDATE_EMAIL
filter, which checks if the email address is in a valid format.