Im just trying to validate an email ending with a particular string. Im using PCRE for this…but cant seem to get it to work. The email needs to end in either boisestate.edu or u.boisestate.edu. I’ve been stabbing at this one for awhile…some help would be really appreciated!
This is what I have so far:
if (preg_match("/[a-zA-Z]+[a-z0-9A-Z_-]+@[\.u]?\.boisestate\.edu$/", "melissa@boisestate.edu", $matches)){
echo "Match was found <br />";
print_r($matches);
}
Instead of trying to do the whole email match and domain check in one, you could just
filter_varand a much simpler regex for domain validation.Example:
When called like this:
You’ll get the output:
As far as the regex is concerned, the reason why yours is failing, is because you’re searching for one of the chars in the square-brackets, only once.
So, you would match things like
foo@.boisestate.eduandfoo@uboisestate.edu. If you use parenthesis instead of square brackets, the?will optionally match the entire contents inside the parens.