Google News
logo
PHP Program to Sanitize and validate an email address
In the following example of PHP program to sanitize and validate an email address using the filter_var() function :
Program :
<?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.";
}
?>
Output :
The email address john.doe@example.com is valid.
In this example, the email address "john.doe@example.com" is first sanitized using the 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.

If the email address is valid, the program outputs "The email address john.doe@example.com is valid." Otherwise, it outputs "The email address john.doe@example.com is not valid."