> This PHP module helps us to remove special characters from defined set elements of which we don’t want to have this special characters.
> For example in validating an email, we would like it not to have characters such as #, $, %, &,^ and many more.
> To enhance this functionality we use filters.
> When we sent data from forms, cookies or any other external sources it is always best practice to validate it through PHP filters.
> To see which filter functionalities PHP offers use the function filter_list.
> Let us say we want to validate an email with unwanted characters, the code below shows this functionality.It therefore tells us if the email is valid or invalid.
Example - Use of PHP filters
$email = "email@example.com";
$email = filter_var($email,FILTER_SANITIZE_EMAIL); //removing unwanted characters from the email
if(!filter_var($email,FILTER_VALIDATE_EMAIL) == false ) // Now checking if the email is valid
echo $email.' is a valid email address..!';
echo $email.'is not a valid email address..!';
You will get Output on browser as shown below;
firstname.lastname@example.org is a valid email address..!
> The function filter-var(),takes two arguments,the first one being the string to Validate and then how you want to validate it. It therefore validates the data and sanitizes it.