2

I need to check if the email id provided by the user ends with aa.bb.cc

For example if the user provides an email id name@collegename.aa.bb.cc i want the check to only be placed on aa.bb.cc collegename can be whatever it does not matter but the id must end with aa.bb.cc

So to achieve it I tried

$value = 'name@collegename.aa.bb.cc';
$explodedEmail = explode('@', $value);
$domain = array_pop($explodedEmail);

This gives me an output of

collegename.aa.bb.cc

So how can I place a check where the collegename is ignored and i have just aa.bb.cc

Saadia
  • 856
  • 1
  • 12
  • 32
  • Take a look at "regular expressions" and how functions like `preg_match()` work. Read its documentation for that, it comes with good examples: http://php.net/manual/en/function.preg-match.php – arkascha Apr 07 '16 at 06:59

4 Answers4

2

You could just use explode (again), this time with a added limit:

$dot = explode('.',$domain,2);

Which prints out

Array 
( 
    [0] => collegename 
    [1] => aa.bb.cc 
)

And then it would be a simple matter of matching against $dot[1]

Epodax
  • 1,828
  • 4
  • 27
  • 32
1

Take a look at "regular expressions" and how functions like preg_match() work. Read its documentation for that, it comes with good examples: http://php.net/manual/en/function.preg-match.php

A simple example would be such code:

<?php
$subject = 'name@collegename.aa.bb.cc';
$pattern = '/^([^@]+)@([^.]+)\.(.+)$/';
preg_match($pattern, $subject, $tokens);
var_dump($tokens);

The output is:

array(4) {
  [0] =>
  string(25) "name@collegename.aa.bb.cc"
  [1] =>
  string(4) "name"
  [2] =>
  string(11) "collegename"
  [3] =>
  string(8) "aa.bb.cc"
}

To make a more specific test against the domain aa.bb.cc and not match anything else you could use a more specific regular expression:

$pattern = '/^([^@]+)@([^.]+)\.aa\.bb\.cc$/';

So regular expressions offer a very flexible tool to separate tokens inside a subject string. To develop more complex matching patterns online regex tools are available for example https://regex101.com/

arkascha
  • 41,620
  • 7
  • 58
  • 90
0

You can use a regular expression to detect this:

preg_match("\aa\.bb\.cc$\", $value)

This return true if the email ends in aa.bb.cc

If you want more information: http://php.net/manual/en/function.preg-match.php

Miguel Jiménez
  • 484
  • 2
  • 12
0

Actually it's pretty trivial

 // Check if email is valid
 if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
   list($user,domain) = explode('@', $email);

   if ($domain === 'yourdomain.com') {
      // here goes logic if domain is valid
   }
 } 

There are also other options:

check if string ends with domain.com with substr() startsWith() and endsWith() functions in PHP

function endsWith($haystack, $needle) {
    // search forward starting from end minus needle length characters
    return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
}

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
   if ($endsWith($email) === 'aa.bb.cc') {
      // here goes logic if domain is valid
   }
 } 

preg_split/preg_match to extract valid domain

But in the most cases first one will work

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85
  • _"how can I place a check where the collegename is ignored and i have just aa.bb.cc"_ So anything before the `aa.bb.cc` doesn't matter. – Epodax Apr 07 '16 at 07:13
  • then option with ends with will work -> http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php – Robert Apr 07 '16 at 07:14