Filter validate email php

Here is an example of a PHP function that filters and validates an email address:

function validateEmail($email) {
  // Regular expression to match most common email address formats
  $regex = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/';

  // Check if the email address matches the regular expression
  if (!preg_match($regex, $email)) {
    return false; // Email address is invalid
  }

  // Check if the email address is not empty
  if (empty($email)) {
    return false; // Email address is empty
  }

  // Check if the email address contains any invalid characters
  if (strpos($email, '@') === false) {
    return false; // Email address does not contain an '@' symbol
  }

  // Check if the email address contains any invalid characters after the '@' symbol
  if (strpos($email, '.') === false) {
    return false; // Email address does not contain a '.' symbol
  }

  // Check if the email address contains any invalid characters before the '@' symbol
  if (strpos($email, '+')!== false || strpos($email, '%')!== false || strpos($email, '_')!== false || strpos($email, '.')!== false || strpos($email, '-')!== false) {
    return false; // Email address contains invalid characters before the '@' symbol
  }

  // If all checks pass, the email address is valid
  return true;
}

You can use this function like this:

$email = '[email protected]';
if (validateEmail($email)) {
  echo 'Email address is valid';
} else {
  echo 'Email address is invalid';
}

This function uses a regular expression to match most common email address formats, and then checks for several specific conditions to ensure that the email address is valid. These conditions include:

You can modify this function to fit your specific needs, such as adding additional checks or modifying the regular expression to match more specific email address formats.

It's also worth noting that this function does not check if the email address actually exists or is in use. It only checks if the email address is in a valid format.