To verify and sanitize an email address in PHP, you can use a combination of regular expressions and built-in functions.
Here is an example of how to verify and sanitize an email address using regular expressions and the filter_var
function:
<?php // Verify that the email address is in a valid format $email = 'user@example.com'; if (preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/', $email)) { // Sanitize the email address $email = filter_var($email, FILTER_SANITIZE_EMAIL); } else { // Invalid email address $email = ''; }
This code first uses a regular expression to verify that the email address is in a valid format. If the email address is valid, it is then sanitized using the filter_var
function with the FILTER_SANITIZE_EMAIL
filter. If the email address is invalid, it is set to an empty string.
It's important to note that this is just one example of how to verify and sanitize an email address in PHP. There are many other approaches and techniques that you can use depending on your specific requirements. Consult the PHP documentation and online resources for more information.