note 44285 deleted from ref.mail by aidan
| From: | aidan@php.net | Date: | Sun, 22 Aug 2004 14:19:12 +0000 |
| Subject: | note 44285 deleted from ref.mail by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75113@lists.php.net to get a copy of this message | ||
Note Submitter: expert at dotgeek dot org
----
Use the following function check_mail() for see if e-mail address is OK, enjoy :-)
<?php
function ereg_words($car, $data){
$err = false;
$cnt = strlen($data);
$len = strlen($car);
for($i=0;$i<$cnt;$i++){
$errm = false;
$chrm = strtolower($data{$i});
for($k=0;$k<$len;$k++) if($car{$k}==$chrm) $errm = true;
if(!$errm) $err = true;
}
return $err;
} // End ereg_words() -----------
function check_mail($mail){
// $car -> list acceptable words
$car = "0123456789.abcdefghijklmnopqrstuvwxyz_@-";
// $ext -> list extension domain words
$ext = "abcdefghijklmnopqrstuvwxyz";
/**
* if you not use return(), is necesary to put elseif()
*/
if(ereg_words($car, $mail)) return "01"; // contain invalid caracter(s)
$expMail = explode("@", $mail);
if(count($expMail)==1) return "02"; // invalid format
if(count($expMail)>2) return "03"; // contain multi @ caracters
else{
if(empty($expMail[0])) return "04"; // begin at @ is empty
if(strlen($expMail[1])<3) return "05"; // after @ invalid format
$expSep = explode(".", $expMail[1]);
if(count($expSep)==1) return "06"; // invalid format domain host
else{
if(empty($expSep[count($expSep)-2])) return "07"; // domain name is empty
if(strlen($expSep[count($expSep)-1])<2 || strlen($expSep[count($expSep)-1])>4) return
"08"; // invalid extension domain
if(ereg_words($ext, $expSep[count($expSep)-1])) return "09"; // extension domain
contain invalid caracter(s)
}
}
return $mail;
} // End check_mail() -----------------------------------
// Testing ...
$mail = "expert@dotgeek.org";
if($mail == $verify = check_mail($mail)) echo "The following E-Mail Address :
<b>".$mail."</b> it is valid format !";
else echo "Error ".$verify." : The following E-Mail Address :
<b>".$mail."</b> it is NOT valid format !";
?>