William R Strong
William R Strong

Reputation: 11

Understanding foreach with arrays

I created a short script that should look at the $email and see if there's an @ symbol and tells you either you have one or not. Then, it should check each of the extensions $protocols array and tell you if there is the extension in the $email. It gets through the first two of $protocols; however, stops cold with no error messages or to continue through the $protocols.

<?
// Set searching info!
    $attsymbol = "@";
    $protocols = array('.com', '.net', '.org', '.biz', '.info', '.edu', '.mil', '.cc', '.co', '.website', '.site', '.tech', '.tv');

// Set email
    $email = "[email protected]";

// check for the @ symbol!
    if (!strpos($email, $attsymbol))
        {
            die ("There is no " . $attsymbol . " in the email address!<br>");
        }
    else
        {
            echo "This is an " . $attsymbol . " in the email address!<br>";
// Check for all of the protocols in the array!

    foreach ($protocols as $protocol)
        {
        echo $protocol . "<br>";
            if (!strpos($email, $protocol))
                {
                    die("There is no " . $protocol . " in the email address!<br>");
                }
            else
                {
                    echo"There is a " . $protocol . " in the email address!<br>";
                }

        }
    }

?>

Thank you in advance for your assistance with this!

Upvotes: 0

Views: 56

Answers (2)

loadiam
loadiam

Reputation: 79

Instead, you can use FILTER_VALIDATE_EMAIL to check if the value is a valid email address.

<?php

$email = "[email protected]";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "<pre>Valid</pre>";
} else {
    echo "<pre>Not Valid</pre>";
}

Upvotes: 1

Progrock
Progrock

Reputation: 7485

You want to loop and look for your needle.

In the following I loop, if I find the needle, I set a flag and break out the loop.

I'm reversing the tlds and the email for the comparison as it's easier to then compare ends of strings.

Example output is for the three given email inputs.

foreach(['jimbob', '[email protected]', '[email protected]'] as $email) {
    $errors = [];
    $tlds   = ['.com', '.net', '.org', '.biz', '.info', '.edu', '.mil', '.cc', '.co', '.website', '.site', '.tech', '.tv'];

    // @ check.
    if (strpos($email, '@') === false) {
        $errors[] = 'No @ in address.';
    }

    // tld check.
    $reversed_tlds  = array_map('strrev', $tlds);
    $reversed_email = strrev($email);
    $found = false;
    foreach($reversed_tlds as $reverse_tld) {
        if(strpos($reversed_email, $reverse_tld) === 0) {
            $found = true;
            break;
        }
    }
    if(!$found) {
        $errors[] = 'Email address must end in one of: ' . implode(',', $tlds);
    }

    var_dump($errors);
}

Output:

array(2) {
  [0]=>
  string(16) "No @ in address."
  [1]=>
  string(102) "Email address must end in one of: .com,.net,.org,.biz,.info,.edu,.mil,.cc,.co,.website,.site,.tech,.tv"
}
array(1) {
  [0]=>
  string(102) "Email address must end in one of: .com,.net,.org,.biz,.info,.edu,.mil,.cc,.co,.website,.site,.tech,.tv"
}
array(0) {
}

Upvotes: 0

Related Questions