View Single Post
Old 05-22-2008, 04:43 PM   #2 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I don't know what you're after exactly. However, I would put the list of acceptable domains in an array, that way you don't have to keep adding to your if statement. I would take the following approach, using objects so that you can extend if need be, and keep it nice and tidy.

php Code:
class Validate_Email
{
    private $m_szEmail;
    private $m_szDomain;
   
    public function __construct($szEmail)
    {
        $this->m_szEmail = $szEmail;
    }
   
    public function hasValidDomain()
    {
        $aAllowedDomains = array('yahoo', 'hotmail', 'msn', 'gmail');
        preg_match('~.+?@(?P<domain>.+?)\..+~', $this->m_szEmail, $aMatches);
       
        $this->m_szDomain = $aMatches['domain'];
   
        if(!in_array($this->m_szDomain, $aAllowedDomains))
        {
            return false;
        }
       
        return true;
    }
   
    public function getDomain()
    {
        return ucfirst($this->m_szDomain);
    }
}

Then you would simply use it like so. I've put this in a loop so we can check an array of them.

php Code:
$aEmails = array('messiah@hotmail.com', 'buddha@hushmail.com', 'horus@yahoo.co.uk');

foreach ($aEmails as $szEmail)
{
    $pEmail = new Validate_Email($szEmail);
   
    if(!$pEmail->hasValidDomain())
    {
        printf('%s is an unacceptable domain.<br />', $pEmail->getDomain());
        continue;
    }
       
    printf('%s is an acceptable domain.<br />', $pEmail->getDomain());
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
webosb (05-22-2008)