note 33456 deleted from function.eregi by cmb

From: Date: Sun, 20 Oct 2019 08:45:09 +0000
Subject: note 33456 deleted from function.eregi by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212683@lists.php.net to get a copy of this message
Note Submitter: tomi at vacilando dot net ---- Keran's solution is a good one but I found it does not work for a small number of domains -- because of a bug in gethostbyname() that causes some valid domains not to resolve properly. The workaround is to test such domains with "www." added to the beginning. This script does all that and works perfectly for me. Enjoy! // Function to validate emails. function validate_email($email_raw) { // replace any ' ' and \n in the email $email_nr = eregi_replace("\n", "", $email_raw); $email = eregi_replace(" +", "", $email_nr); $email = strtolower( $email ); // do the eregi to look for bad characters if( !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*". "@([a-z0-9]+([\.-][a-z0-9]+))*$",$email) ){ // okay not a good email $feedback = 'Error: "' . $email . '" is not a valid e-mail address!'; return $feedback; } else { // okay now check the domain // split the email at the @ and check what's left $item = explode("@", $email); $domain = $item["1"]; if ( ( gethostbyname($domain) == $domain ) ) { if ( gethostbyname("www." . $domain) == "www." . $domain ) { $feedback = 'Error: "' . $domain . '" is most probably not a valid domain!'; return $feedback; } // ? $feedback = "valid"; return $feedback; } else { $feedback = "valid"; return $feedback; } } }

« previous php.notes (#212683) next »