Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
FILTER_SANITIZE_EMAIL constant in PHP
The FILTER_SANITIZE_EMAIL constant removes all characters from a string that are not allowed in an email address according to RFC standards. This filter is useful for cleaning user input before validation or storage.
Syntax
filter_var($email, FILTER_SANITIZE_EMAIL)
Parameters
The filter_var() function accepts the following parameters when used with FILTER_SANITIZE_EMAIL −
- $email − The email string to be sanitized
- FILTER_SANITIZE_EMAIL − The filter constant that removes illegal email characters
Return Value
Returns the sanitized email string with illegal characters removed, or FALSE on failure.
Example
The following example demonstrates how FILTER_SANITIZE_EMAIL removes invalid characters from an email address −
<?php
$myemail = "abc@demo//.com";
$sanitized = filter_var($myemail, FILTER_SANITIZE_EMAIL);
echo "Original: " . $myemail . "<br>";
echo "Sanitized: " . $sanitized;
?>
Original: abc@demo//.com Sanitized: abc@demo.com
Multiple Invalid Characters
Here's an example showing sanitization of an email with multiple types of invalid characters −
<?php
$email = "user@exa<mple>.c om!";
$clean_email = filter_var($email, FILTER_SANITIZE_EMAIL);
echo "Original: " . $email . "<br>";
echo "Sanitized: " . $clean_email;
?>
Original: user@exa<mple>.c om! Sanitized: user@example.com
Conclusion
FILTER_SANITIZE_EMAIL effectively removes invalid characters from email strings, making them RFC−compliant. Always combine sanitization with validation using FILTER_VALIDATE_EMAIL for complete email processing.
