domain for email

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

domain for email

Post by jaymoore_299 »

if I an email, something@aaa-bbb_ccc.com , how do I get only the domain part and excluding the first part up to the @ ?
User avatar
hopfateam
Forum Newbie
Posts: 12
Joined: Sun May 29, 2005 6:30 pm
Location: Germany
Contact:

Re: domain for email

Post by hopfateam »

Code: Select all

$email_adress="something@something.de";
$explode_email=explode("@",$email_adress);
$domain=$explode_email[1];
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

But user@group@domain is valid too...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I think google would bring up a nice array of regex for you here but here's a partially decent one...

Code: Select all

function get_email_parts($address)
{

    if (preg_match('/^([a-z0-9][\w\-\.]*[a-z0-9])@([a-z0-9][\w\-\.]*[a-z0-9])$/i', $address, $matches))
    {
        return array('local_part' => $matches[1], 'domain_part' => $matches[2]); //Return array
    }
    else
    {
        return false; //Doesn't fit the pattern
    }

}
This will do for now wrote: <?php
function get_email_parts($address)
{

    if (
preg_match('/^([a-z0-9][\w\-\.]*[a-z0-9])@([a-z0-9][\w\-\.]*[a-z0-9])$/i', $address, $matches))
    {
        return array(
'local_part' => $matches[1], 'domain_part' => $matches[2]); //Return array
    
}
    else
    {
        return
false; //Doesn't fit the pattern
    
}

}
?>
EDIT | WTF happened to our tags :(
Post Reply