0
votes

I wants restrict user to not to add some popular email service provider domain like gmail.com, yahoo.com, ymail.com, hotmail.com

for that I have created an array

$invalidDomain = ['gmail.com', 'yahoo.com', 'ymail.com', 'hotmail.com']

and then check user input with in_array

if(in_array($insertDomain, $invalidDomain)){
 //restrict
}

but now I also want to check for gmail.co.in, hotmail.co.uk

how can I?

3
i think you should used this ['gmail', 'yahoo', 'ymail', 'hotmail']Bilal Ahmed
okay then I need check for substr right?Ruchi
split the $insertdomain using PHP Split function and then used if conditionBilal Ahmed
i think you understand my point....Bilal Ahmed
Yes. Great. ThanksRuchi

3 Answers

2
votes

You can use regular expressions to achieve this - it will give you more flexibility eg: if you would like to exclude gmail.co.uk but allow gmail.com. See the code snippet below:

$insertDomain = "gmail.com";
$invalidDomain = ['gmail\.[a-zA-Z\.]{2,}', 'yahoo\.[a-zA-Z\.]{2,}', 'ymail\.[a-zA-Z\.]{2,}', 'hotmail\.[a-zA-Z\.]{2,}'];

// join regexp
if (preg_match('/^'.implode("$|^", $invalidDomain).'$/', $insertDomain)) {
    // restrict
    echo $insertDomain."\n";
}
1
votes

Used this type of code

$invalidDomain = ['gmail', 'yahoo', 'ymail', 'hotmail']

and finally in the condition

//$insertDomain = "gmail";

    if(in_array($insertDomain, $invalidDomain)){
     //restrict
    }
0
votes

You could use PHP's parse_url to determine what domain is being used http://php.net/manual/en/function.parse-url.php

The output array would contain host index that would give you the domain / sub-domain used within the string you pass as an argument to that function .i.e.

$partials = parse_url('https://google.com/?id=123');

$insertDomain = $partials['host']; // google.com