Jump to content

Php restrict emails


ronbryce

Recommended Posts

If you want to use just the first one can do something like this.

<?php
$email = "john@test.com, peter@test.com";

if(strstr($email, ',')){
$exploded = explode(',',$email);
$email = trim($exploded[0]);	
}
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
    echo $email; 
}else{
    echo $email." Failed";
}
?>
Edited by QuickOldCar
Link to comment
Share on other sites

I think he wants to specifically restrict the domain of the addresses.

 

Of course the obvious approach would be to simply split at the “@” sign. Note, however, that you need to split at the rightmost “@”, because this symbol is also allowed as a literal character within the local part. For example, foo\@bar@example.com is a valid address.

<?php

$address = 'foo@example.com';
$host = substr(strrchr($address, '@'), 1);

var_dump( $host );

Alternatively, you could use an actual parser library. There are plenty on GitHub.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.