Verifier email php array

Here is an example of how you can verify an email address using PHP and an array:

<?php

// Define an array of valid email domains
$valid_domains = array('gmail.com', 'yahoo.com', 'hotmail.com');

// Get the email address from the user
$email = $_POST['email'];

// Check if the email address is valid
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  // Split the email address into local part and domain
  list($local, $domain) = explode('@', $email);

  // Check if the domain is in the list of valid domains
  if (in_array($domain, $valid_domains)) {
    // Email address is valid and has a valid domain
    echo "Email address is valid";
  } else {
    // Email address is valid, but domain is not in the list of valid domains
    echo "Email address is valid, but domain is not recognized";
  }
} else {
  // Email address is not valid
  echo "Email address is not valid";
}

?>

This code uses the filter_var() function to validate the email address, and then splits the email address into local part and domain using the explode() function. It then checks if the domain is in the list of valid domains using the in_array() function.

You can also use a regular expression to validate the email address, here is an example:

<?php

// Define a regular expression to validate the email address
$regex = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/';

// Get the email address from the user
$email = $_POST['email'];

// Check if the email address matches the regular expression
if (preg_match($regex, $email)) {
  // Email address is valid
  echo "Email address is valid";
} else {
  // Email address is not valid
  echo "Email address is not valid";
}

?>

This code uses the preg_match() function to check if the email address matches the regular expression. The regular expression is designed to match most common email address formats.

You can also use a combination of both methods to validate the email address, for example:

<?php

// Define an array of valid email domains
$valid_domains = array('gmail.com', 'yahoo.com', 'hotmail.com');

// Define a regular expression to validate the email address
$regex = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/';

// Get the email address from the user
$email = $_POST['email'];

// Check if the email address is valid
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  // Split the email address into local part and domain
  list($local, $domain) = explode('@', $email);

  // Check if the domain is in the list of valid domains
  if (in_array($domain, $valid_domains)) {
    // Email address is valid and has a valid domain
    echo "Email address is valid";
  } else {
    // Email address is valid, but domain is not in the list of valid domains
    echo "Email address is valid, but domain is not recognized";
  }
} elseif (preg_match($regex, $email)) {
  // Email address is not valid, but matches the regular expression
  echo "Email address is not valid, but matches the regular expression";
} else {
  // Email address is not valid
  echo "Email address is not valid";
}

?>

This code uses a combination of both methods to validate the email address, it first checks if the email address is valid using the filter_var() function, and then checks if the domain is in the list of valid domains using the in_array() function. If the email address is not valid, it then checks if it matches the regular expression using the preg_match() function.