Click to See Complete Forum and Search --> : Stripping Down Email
corpking
06-15-2006, 02:49 PM
I'm new to PHP (well programming all together) and would really appreciate some help on this.
I have a registration form where users are going to enter their email addresses, and their domains have to be exist in a safe-list domain or else they can't proceed.
Now some users are going to have emails that will have sub-domains (john@accounting.xyz.com or jane@marketing.xyz.com), what would be the best way to handle that situation?
I have the following code so far to handle regular email addresses:
list($username, $domain) = split('@', $email);
And then I run my query with $domain to make sure it exists in the table. But if it were a sub-domain address, it would look for either "accounting.xyz.com" or "marketing.xyz.com" from the example above, where as we will only have xyz.com stored.
Ideally, I'd like to even break out the extension as a seperate fields in the table. Thanks for any and all help on this.
- Jay
bokeh
06-15-2006, 04:36 PM
what would be the best way to handle that situation?From what point of view? Why are you doing this? What are trying to achieve?
corpking
06-15-2006, 05:00 PM
I'm going to have a safe-list of clients that will access the site. So I'll have a list of about 100 domains in a table that can access the site, but a bunch of them have funky email setups like jane@mail.company.com or bob.ross@research.organization.com.
So I need to break apart to emails in a way that after "@" split, it needs to split the underlined portions above by the dots, and take the last string as extension, and second to last as domain. I think that logic makes sense, but haven't the slightest clue how to implement it :).
Then I just simplay look in my table for company.com and organization.com to see if that client's domain is in my safe-list. Anyhow, let me know if that makes more sense. Thanks a bunch! Jay.
corpking
06-15-2006, 05:11 PM
Noticed it underlined the entire email address above.
Just to clarify, I need to break apart the email as follows:
jane @ mail . company . com
Split after the "@" so I can get the mail . company . com alone.
Then chop mail . company . com up by the "." and take the bold (com) string as the domain-extension, and the italicized (company) as the domain, and the underline (mail) I don't really care about.
There we have it. Thanks for any help in advance. Jay.
NogDog
06-15-2006, 06:04 PM
See if this (untested) code does the trick:
$domainParts = explode(".", array_pop(explode("@", $email)));
while(count($domainParts) > 2)
{
array_shift($domainParts);
}
$domain = implode(".", $domainParts);
bokeh
06-15-2006, 06:09 PM
I need to break apart the email as follows:If only things were so simple... but they're not. To break a domain name down in such a way assumes all domain names are equal but that is just not true. For example domain.co.uk.
NogDog
06-15-2006, 06:31 PM
// assume $domain contains the portion of the email after the "@"
$validDomains = array("domain.com",
"another.com",
"british.co.uk",
"canadian.co.ca");
$found = FALSE;
foreach($validDomains as $value)
{
if(preg_match('/^([^.]\.)*' . $value . '$/i', $domain))
{
$found = TRUE;
break;
}
}
if($found)
{
// valid email domain
}
else
{
// invalid email domain
}
bokeh
06-15-2006, 06:49 PM
Just looking at this:if(preg_match('/^([^.]\.)*' . $value . '$/i', $domain)) I think a combination of strtolower() and strpos() would be a good alternative or stripos() for PHP5
NogDog
06-15-2006, 06:57 PM
I would think strpos() would not make it clear that there are no additional characters after the matching part of the domain, though.
bokeh
06-15-2006, 07:14 PM
I would think strpos() would not make it clear that there are no additional characters after the matching part of the domain, though.True. I just suggested that because it's fast. There are string functions that could get that info though, strstr for example which returns the rest of the string but that's probably as slow as the regex.
NogDog
06-15-2006, 08:15 PM
Yeah, if we're talking a handfull of domains, I wouldn't worry about the preg performance. But if the list is - or could become - sizeable, then I'd definitely look at an alternative along the lines you're suggesting.
corpking
06-15-2006, 09:53 PM
NogDog -- This did the trick! Thanks so much for the help! I really appreciate it :).
Bokeh -- Thanks for your pointers, I actually looked through my list and noticed that for now, I've got no international domains to worry about. That could change rather quickly and I'll probably be posting about that soon asking for clarifications on the code samples you guys exchanged above :)! Thanks again!
- Jay
bokeh
06-16-2006, 04:05 AM
I'd definitely look at an alternative along the lines you're suggesting.Yes, obviously it depends on the size. I guess you could use both; strpos for speed and preg_match for precission.if(strpos() and preg_match()) That way the regex would only be used if a positive were found for strpos.