You can validate an email in PHP using regular expressions. Here's an example code you can use:

Code:
$email = "[email protected]";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo "Invalid email format";
} else {
  echo "Valid email format";
}

This code uses the filter_var function with the FILTER_VALIDATE_EMAIL flag to validate the email. If the email is valid, it will output "Valid email format". If it is not valid, it will output "Invalid email format".
 
You can validate an email in PHP using regular expressions. Here's an example code you can use:

Code:
$email = "[email protected]";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo "Invalid email format";
} else {
  echo "Valid email format";
}

This code uses the filter_var function with the FILTER_VALIDATE_EMAIL flag to validate the email. If the email is valid, it will output "Valid email format". If it is not valid, it will output "Invalid email format".
How to make so that only gmail and proton are considered valid?
 
How to make so that only gmail and proton are considered valid?
To validate that an email belongs to a specific domain, in this case, Gmail or Proton, you can check the domain part of the email address. Here's an example code you can use:

Code:
$email = "[email protected]";

$allowed_domains = ["gmail.com", "protonmail.com"];

$email_parts = explode("@", $email);

if (!filter_var($email, FILTER_VALIDATE_EMAIL) || !in_array($email_parts[1], $allowed_domains)) {
  echo "Invalid email format or not a Gmail/Proton email address";
} else {
  echo "Valid email format and a Gmail/Proton email address";
}

This code first checks that the email is in a valid format using the FILTER_VALIDATE_EMAIL flag of the filter_var function. Then, it splits the email into an array of parts at the "@" symbol using explode. Finally, it checks that the domain part of the email is in the list of allowed domains using in_array. If the email is invalid or is not a Gmail/Proton email address, it will output "Invalid email format or not a Gmail/Proton email address". If the email is valid and belongs to one of the allowed domains, it will output "Valid email format and a Gmail/Proton email address".
 
Code:
$email = "[email protected]";

$allowed_domains = ["gmail.com", "protonmail.com"];

$email_parts = explode("@", $email);

if (!filter_var($email, FILTER_VALIDATE_EMAIL) || !in_array($email_parts[1], $allowed_domains)) {
  echo "Invalid email format or not a Gmail/Proton email address";
} else {
  echo "Valid email format and a Gmail/Proton email address";
}
Refactor it please
 
Refactor it please
Sure, here's a refactored version of the code that uses a function to check if an email is valid and belongs to a specific domain:

Code:
function is_valid_email($email, $allowed_domains) {
  $email_parts = explode("@", $email);
  return filter_var($email, FILTER_VALIDATE_EMAIL) && in_array($email_parts[1], $allowed_domains);
}

$email = "[email protected]";

$allowed_domains = ["gmail.com", "protonmail.com"];

if (is_valid_email($email, $allowed_domains)) {
  echo "Valid email format and a Gmail/Proton email address";
} else {
  echo "Invalid email format or not a Gmail/Proton email address";
}

This code defines a reusable function is_valid_email that takes two arguments - an email and a list of allowed domains. The function splits the email into parts and checks if it is valid and belongs to one of the allowed domains. The main part of the code then calls this function with the email and the allowed domains list and outputs the result.
 
Sure, here's a refactored version of the code that uses a function to check if an email is valid and belongs to a specific domain:

Code:
function is_valid_email($email, $allowed_domains) {
  $email_parts = explode("@", $email);
  return filter_var($email, FILTER_VALIDATE_EMAIL) && in_array($email_parts[1], $allowed_domains);
}

$email = "[email protected]";

$allowed_domains = ["gmail.com", "protonmail.com"];

if (is_valid_email($email, $allowed_domains)) {
  echo "Valid email format and a Gmail/Proton email address";
} else {
  echo "Invalid email format or not a Gmail/Proton email address";
}

This code defines a reusable function is_valid_email that takes two arguments - an email and a list of allowed domains. The function splits the email into parts and checks if it is valid and belongs to one of the allowed domains. The main part of the code then calls this function with the email and the allowed domains list and outputs the result.
Niice, you're so cool
 
Chat
Rooms will appear here
Connecting...
Connecting...
Run command
Edit message
  • Insert:

Members online

No members online now.