Cesar Bielich
Cesar Bielich

Reputation: 4945

How to find out if an email address starts with something and ends with a domain

Take this email for example

$email = "[email protected]";

I need to use php to find out if an email starts with outlook_ AND ends with @outlook.com. Since 75AA07B2DF4B8074 will always be different. I've got the beginning but not sure how to get the end as well.

if (strpos($email, 'outlook_') === 0) {

}

Upvotes: 1

Views: 416

Answers (2)

Nick
Nick

Reputation: 147256

You could use strpos with a negative offset to search for the trailing @outlook.com. Since that has a fixed length of 12 characters, you can use an offset of -12 to strpos to only look for @outlook.com in the last 12 characters; thus it will only match if those characters are exactly @outlook.com. For example:

$email = "[email protected]";
if (strpos($email, 'outlook_') === 0 && strpos($email, '@outlook.com', -12) !== false) {
    echo "outlook email address\n";
}

Upvotes: 2

gioaudino
gioaudino

Reputation: 575

preg_match('/^outlook_.*@outlook\.com$/', $email);

Upvotes: 2

Related Questions