MTT
MTT

Reputation: 318

How to check if email domain is a gmail in php and then strip out "@gmail.*"?

This is a two-part question. Help on either (or both) is appreciated!

1) What is the best php method for checking if an email string is a Gmail address

2) How to strip out everything but the username?

Thanks!

Upvotes: 3

Views: 12622

Answers (4)

Gurinder singh
Gurinder singh

Reputation: 21

For Multiple Emails

$expressions = 
"/(gmail|googlmail|yahoo|hotmail|aol|msn|live|rediff|outlook|facebook)/";
if (preg_match($expressions, $input_email)) {
    throw error   
}

Upvotes: 2

GorillaMoe
GorillaMoe

Reputation: 4134

if (preg_match("/gmail.com/",$email_address)) {
  $email_address = str_replace("@gmail.com","",$email_address);
}

Upvotes: 1

Jim
Jim

Reputation: 18853

Dunno about best method, but here is one method for checking a gmail address using stristr.

if (stristr($email, '@gmail.com') !== false) {
    echo 'Gmail Address!';
}

As for pulling out the username there are a ton of functions as well, one could be explode:

$username = array_shift(explode('@', $email));

There are many ways to do it, the best depends on your needs.

Upvotes: 2

seriousdev
seriousdev

Reputation: 7656

list($user, $domain) = explode('@', $email);

if ($domain == 'gmail.com') {
    // use gmail
}

echo $user;
// if $email is [email protected] then $user is toto

Upvotes: 18

Related Questions