I need to write a function for a project i\'m working on for fun, where we\'re making a site only accessible to students, staff, and alumni at an institution.
Let\'s
Note that just getting everything after the @ may not accomplish what you are trying to accomplish because of email addresses like user@students.ecu.edu. The get_domain function below will only get the domain down to the second level domain. It will return "unc.edu" for username@unc.edu or username@mail.unc.edu. Also, you may want to account for domains with country codes (which have top level domains of 2 characters).
You can use the function below to extract the domain. Then you can use an array of school domains, or put the school domains in a database and check the email address against that.
function get_domain($email)
{
if (strrpos($email, '.') == strlen($email) - 3)
$num_parts = 3;
else
$num_parts = 2;
$domain = implode('.',
array_slice( preg_split("/(\.|@)/", $email), - $num_parts)
);
return strtolower($domain);
}
// test the function
$school_domains = array('unc.edu', 'ecu.edu');
$email = 'someone@students.ecu.edu';
if (in_array(get_domain($email), $school_domains))
{
echo "good";
}