Array variable

hi guys

Basically what i am trying to do is assign a few domain names to one variable like this

$companyEmail = array("@test.com", "@test.co.uk", "@test.org");

this what i have as my code which is supposed to validate an email entered by a user to check if it belongs to any of the domains within the array

$email_regex = '/^[a-zA-Z0-9\\._%+-]+@[a-zA-Z0-9\\.-]+\\.[a-zA-Z]{2,4}$/';
$company_regex = "/$companyEmail$/";

when i test this code it does not recognise any of the domains entered in the array…some help would be appreciated :slight_smile:

Try
echo $CompanyEmail[0];

i have studied arrays a bit more and i realised just having $companyEmail by itself will not print anything but “Array” i need some kind of loop to print them all

anyway i originally wanted to use the array for validation purposes not to be printed

so i tried to add a loop in my code


$companyEmail = array();
$companyEmail[] = 'mytest.com';
$companyEmail[] = 'mytest.co.uk';
$companyEmail[] = 'mytest.org';

$email_regex = "/^[a-zA-Z0-9\\._%+-]+@[a-zA-Z0-9\\.-]+\\.[a-zA-Z]{2,4}$/";
	foreach ($companyEmail as $key => $value)
    $company_regex = "/@$value$/";

    // check the username is allowed (not already taken and valid for whatever)
    if (isset($_REQUEST['username'])) {
      if (!preg_match($company_regex, $_REQUEST['username']) ||
                 !preg_match($email_regex, $_REQUEST['username'])) {
        $validation_message .= "<li>Username must be a valid $companyName email address.</li>";
        $validated = FALSE;
      } else if (!check_user_name($conn, $_REQUEST['username'], $DEBUG)) {
        $validation_message .= '<li>Username taken please try another.</li> ';
        $validated = FALSE;
      }
    }

now this is where it gets really strange :blush::blush::blush: that code above recognises an email i put in with “mytest.org” but does not recognise an email i enter with “mytest.com” or “mytest.co.uk

so it is recognising array[2] but not array[0] or array[1], basically it always only recognises the last array in the list :frowning:
:shifty::shifty: im quite lost now

If you are using the @ symbol in the domains like you have posted in your code, no, it will not work.

$companyEmail = array("test.com", "test.co.uk", "test.org");

On the emails regex, try changing ‘/^ at the start to "^ and $/’ at the end to $"

mari, your code doesn’t perform any checking, only assigning variables

just tried that it still tells me the emails are invalid so it isnt picking up the domains i have entered in the array

that isnt the whole code but that is the part im having an issue with, it worked before where i had only one domain applied to it like this

 $email_regex = '/^[a-zA-Z0-9\\._%+-]+@[a-zA-Z0-9\\.-]+\\.[a-zA-Z]{2,4}$/';
$company_regex = '/@test\\.com$/';

but i need it to check through multiple domain names… i thought an array applied to a variable will be the best method to do this? :shifty::shifty:

in the array where i have ‘test.com’ is that okay or would it need to be ‘test\.com’ :confused:


<?php
function emailInDomain($email, $domains) {
	if (!is_array($domains)) {
		trigger_error("Domain list must be supplied as an array.", E_USER_ERROR);
	}
	if (preg_match("/^([.0-9a-z_-]+)@(([0-9a-z-]+\\.)+[0-9a-z]{2,4})$/i", trim($email), $matches)) {
		if (in_array($matches[2], $domains)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}


$domains = array("test.com", "test.org");

$email1 = "test@test.org";
$email2 = "test@test.net";


if (emailInDomain($email1, $domains)) {
	echo $email1." is a valid email.";
} else {
	echo $email1." is an invalid email.";
}
// reports as being a valid email, checked against the supplied domain list.


if (emailInDomain($email2, $domains)) {
	echo $email2." is a valid email.";
} else {
	echo $email2." is an invalid email.";
}
// reports as being an invalid email, checked against the supplied domain list.
?>

arrays intended to be used only through loop
loop over array, fetch values one by one and use these value to check.

do you have a code that check only one domain? If not, you have to start it first

im currently using this

 $email_regex = '/^[a-zA-Z0-9\\._%+-]+@[a-zA-Z0-9\\.-]+\\.[a-zA-Z]{2,4}$/';
    $company_regex = '/@test\\.com$/';

    // check the username is allowed (not already taken and valid for whatever)
    if (isset($_REQUEST['username'])) {
      if (!preg_match($company_regex, $_REQUEST['username']) ||
                 !preg_match($email_regex, $_REQUEST['username'])) {
        $validation_message .= "<li>Username must be a valid $companyName email address.</li>";
        $validated = FALSE;
      } else if (!check_user_name($conn, $_REQUEST['username'], $DEBUG)) {
        $validation_message .= '<li>Username taken please try another.</li> ';
        $validated = FALSE;
      }
    }

this works fine, i just need to replace test.com with a variable that contains more than one domain

Why use two expressions to determine if the email is valid and if the email domain is in the allowed array?


function emailInDomain($email, $domains) {
	if (!is_array($domains)) {
		trigger_error("Domain list must be supplied as an array.", E_USER_ERROR);
	}
	if (preg_match("/^([.0-9a-z_-]+)@(([0-9a-z-]+\\.)+[0-9a-z]{2,4})$/i", trim($email), $matches)) {
		if (in_array($matches[2], $domains)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

This function would first validate the supplied email address using preg_match(). If the email is in proper email format, this would then match the domains with $matches[2] which is the domain chunk of the validated email address using in_array($matches[2], $domains) where $domains is a supplied array of all domains allowed by the application.

To add it to your existing code:

 $companyEmail = array("test.com", "test.co.uk", "test.org");

    // check the username is allowed (not already taken and valid for whatever)
    if (isset($_REQUEST['username'])) {
      /** replace
       * if (!preg_match($company_regex, $_REQUEST['username']) ||
       *         !preg_match($email_regex, $_REQUEST['username'])) {
       */

      ## with:
      if (!emailInDomain($_REQUEST['username'], $companyEmail)) {
      ##

        $validation_message .= "<li>Username must be a valid $companyName email address.</li>";
        $validated = FALSE;
      } else if (!check_user_name($conn, $_REQUEST['username'], $DEBUG)) {
        $validation_message .= '<li>Username taken please try another.</li> ';
        $validated = FALSE;
      }
    }

its still not recognising the domains :blush:

could there be something wrong with the way the array is set up?

i would have thought it would work the way i previously had it because it works with a single variable :nono:

i took the @ symbol out and used this

 
$companyEmail = array("test.co.uk");

$email_regex = '/^[a-zA-Z0-9\\._%+-]+@[a-zA-Z0-9\\.-]+\\.[a-zA-Z]{2,4}$/';
$company_regex = "/@$companyEmail$/";

this still does not recognise the domain and tells me i need a valid domain. If i take the array part out of the variable it works fine :rolleyes: so what is it that makes array behave differently to a normal variable :nono:

could it be because the variable is external so in my function i am using global $companyName; to call the variable at the moment, should this still work if the variable is an array?

anyone??

okay so im trying to figure out why this isnt working, and i tried to print out the array variable and it just prints “Array” instead of the names of the domains i have assigned to it :nono::nono:

what am i doing wrong? :frowning:

Once again… This works.


function emailInDomain($email, $domains) {
	if (!is_array($domains)) {
		trigger_error("Domain list must be supplied as an array.", E_USER_ERROR);
	}
	if (preg_match("/^([.0-9a-z_-]+)@(([0-9a-z-]+\\.)+[0-9a-z]{2,4})$/i", trim($email), $matches)) {
		if (in_array($matches[2], $domains)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

$companies = array("test.com", "test.net", "test.org");

if (isset($_REQUEST['username'])) {
	if (!emailInDomain($_REQUEST['username'], $companies)) {
		$valication_message .= "<li>Username must be a valid $companyName email address.</li>";
		$validated = FALSE;
	} else if {(!check_user_name($conn, $_REQUEST['username'], $DEBUG)) {
		$validation_message .= "<li>Username taken please try another.</li>";
		$validated = FALSE;
	}
}

Using “me@test.com” would validate, checking email syntax and email domain. “me@anothertest.com” would not validate since “anothertest.com” is not the specified valid domains.