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?
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."
";
}
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
Used this type of code
$invalidDomain = ['gmail', 'yahoo', 'ymail', 'hotmail']
and finally in the condition
//$insertDomain = "gmail";
if(in_array($insertDomain, $invalidDomain)){
//restrict
}