John Davidson

php - in_array not working with my array in foreach

0 comments
Message:


I want to create a small script to get every mail with specifics domain name


Here is my code


// $_POST
$email_list_explode = explode("\n",$_POST['email_list']);
$ndd_accepted = explode(",",$_POST['ndd']);

// ARRAY OF GOOD EMAIL
$email_good = array();

foreach ($email_list_explode as $email_list_result){

// GETTING EMAIL DOMAIN
$domain_name = substr(strrchr($email_list_result, "@"), 1);

// CHECK IF THE EMAIL DOMAIN IS IN THE ARRAY OF ACCEPTED DOMAIN NAME
if (in_array($domain_name, $ndd_accepted)){

// PUSHING GOOD EMAIL IN THE ARRAY OF GOOD EMAIL
array_push($email_good, $email_list_result);
}
}

// PRINTING ARRAY OF GOOD EMAIL
print_r($email_good);

Example of $_POST


$_POST['email_list'] = [email protected]
[email protected]
$_POST['ndd'] = gmail.com,orange.fr;

My $email_good array should be [email protected]



My Answer:
",", $_POST['email_list']);
$domain = "example.com";
$emails = [];

foreach($email_list_explode as $email){
if(strpos($email, $domain) !== false){
$emails[] = $email;
}
}

foreach($emails as $email){
echo $email . "
";
}

Rate this post

5 of 5 based on 3043 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us