wildcards in php for email check function

i am currently developing login and register system on my local machine for test purposes and, now, i am in phase of email checking function. as you know, you can use php build in function for that, that is not 100% correct. otherway, i want that only @gmail.com , @hotmail.* , @yahoo.* and emails like that to be accepted, not others (i.e. @somemail.net).

so i want to develop code that should look like:

    <?

    ...

    require_once 'emailformcheck.php';

    if ($email === '@gmail.com' || $email === '@yahoo.com'...) {

        require_once 'emaildbcheck.php';

        require_once 'passwordchecker.php';

}

...

?>

but instead of typing @gmail.com or @yahoo.* , i want to use some wild cards instead of that, like:

*@gmail.com or *@yahoo.com . can anybody tell me how i need to format that, true is, i am not really new in php programming but i still learn. anybody knows some wildcard for replacing string data in php file?

Upvotes: 1

Views: 723

Answers (2)

Benjamin Kaiser
Benjamin Kaiser

Reputation: 2227

First run this function to check if the email is valid:

$isValid = filter_var($email, FILTER_VALIDATE_EMAIL);

Then to check the domain run the following:

list ($user, $domain) = explode('@', $email);
list ($domain, $tld) = explode('.', $domain);

Where $user conains the data before the @, domain contains the domain excluding the TLD (e.g. gmail) and $tld contains the first part of the TLD (e.g. com).

You can then check for gmail.com with this condition:

if($domain == "gmail" && $tld == "com")

and to check for wildcards like hotmail use:

if($domain == "hotmail")

Clearly using wildcards you are going to run into subdomain problems such as someone making a domain: hotmail.mywebsite.com and their email being [email protected].

Upvotes: 1

arkascha
arkascha

Reputation: 42935

Use regular expressions for such pattern search in strings. PHP brings the preg extension. It is well documented, so I suggest you take a look at the documentation and the millions of examples you find on the web:

Using that you can write your conditional as something like:

if ( preg_match($email, '^[^@]+@((gmail\.com)|(yahoo\.com))$') )

Upvotes: 1

Related Questions