Reputation: 907
I need to get just the domain name from an email address - so if the email address is [email protected] I need to grab just the gmail part.
I have the following code that works and returns gmail for a [email protected] email address but returns co for a [email protected] email address.
$user_register_email = "[email protected]";
$domain = preg_replace( '!^.+?@(.+?)$!', '$1', $user_register_email );
$domain = explode('.', $domain);
$domain = array_reverse($domain);
$domain = "$domain[1]";
echo $domain;
Can anyone tell me how I amend the above code so it will also work for gmail.co.uk addresses please?
Many thanks,
John
Upvotes: 1
Views: 6072
Reputation: 47179
explode
and reverse
functions on the array are not necessary, preg_replace
is fine by itself:
$domain = preg_replace( '!^.+?([^@]+)$!', '$1', $user_register_email );
Output:
gmail.co.uk
Upvotes: 0
Reputation: 2166
$user_register_email = "[email protected]";
$domain = explode('@', $user_register_email);
$domain = explode('.', $domain[1]);
echo $domain[0];
You can use 2 explodes
Upvotes: 5
Reputation: 133370
You can isolate the domain part (string after the @) in this way
$mystring = substr(user_register_email ,strpos($user_register_email , '@') +1, 255);
and the perform the explode
Upvotes: 0