0

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 = "bob@email.website";

// 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!

  • `die` causes your program to exit on the first `protocol` that is not present in the email address. – Nick Nov 01 '20 at 22:59
  • 1
    .com, .net etc is not a protocol.. why are you not using `filter_var($email, FILTER_VALIDATE_EMAIL)`? as-is something like `@@email.website@@` would pass your checks – Lawrence Cherone Nov 01 '20 at 23:07
  • Thanks for the die suggestion. I will change that right away. As far as the filter_var suggestion, I've done some preliminary research and only get short blurbs as to its use. Can you please site an example of how it is written and used? – William R Strong Nov 01 '20 at 23:10
  • Does this answer your question? [How to validate an email address in PHP](https://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php) – Jeto Nov 01 '20 at 23:33

2 Answers2

1

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

<?php

$email = "bob@email.website";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "<pre>Valid</pre>";
} else {
    echo "<pre>Not Valid</pre>";
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
loadiam
  • 79
  • 4
0

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', 'bob@example.uk', 'bob@email.website'] 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) {
}
Progrock
  • 7,373
  • 1
  • 19
  • 25