![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
[PHP]
<?php
define('DV_ERR_UNKNOWN',0);
define('DV_ERR_TOO_SHORT',1);
define('DV_ERR_TOO_LONG',2);
define('DV_ERR_ILLEGAL_CHARS',3);
define('DV_ERR_DIGIT_NOENOUGH',4);
class DV
{
var $errtype;
var $errstr;
function DV()
{
$this->errtype = false;
$this->errstr = '';
}
/*
*method: is_cn_tel ($tel)
*args: $tel (value to test)
*note: igore any parenthese, dashes, dots, or spaces
*/
function is_cn_tel($tel)
{
/*replace parentheses, dashes, dots, or spaces with*/
$new_tel = ereg_replace("[\(\)\. - _]", "", $tel);
$len = strlen($new_tel);
if ($len < 11)
{
$this->errtype = DV_ERR_TOO_SHORT;
$this->errstr = "电话号码必须是11位数字.('$tel' "."是 $len 位数字.)";
return false;
}
if ($len > 11)
{
$this->errtype = DV_ERR_TOO_LONG;
$this->errstr = "电话号码必须是11位数字.('$tel' "."是 $len 位数字.)";
return false;
}
/*make sure there are only digits left*/
if(ereg("[^0-9]", $new_tel))
{
$this->errtype = DV_ERR_ILLEGAL_CHARS;
$this->errstr = "'$tel' 是无效的.电话号码应该只有数字.";
return false;
}
return true;
}
/*
*method: is_cn_zip ($zip)
*args: $zip (value to test)
*note: tests $zip to see if it looks like chn zip code(six digits)
*/
function is_cn_zip($zip)
{
$len = strlen($zip);
if ($len < 6)
{
$this->errtype = DV_ERR_TOO_SHORT;
$this->errstr = "邮政编码必须是6位数字.('$zip' "."是 $len 位数字)";
return false;
}
if ($len > 6)
{
$this->errtype = DV_ERR_TOO_LONG;
$this->errstr = "邮政编码必须是6位数字.('$zip' "."是 $len 位数字)";
return false;
}
if (!ereg("^[0-9]{6}$", $zip))
{
$this->errtype = DV_ERR_ILLEGAL_CHARS;
$this->errstr = "'$zip'是无效的.邮政编码应该只有数字.";
return false;
}
return true;
}
/*
*method: is_hostname ($host)
*args: $host (value to test)
*note: tests $host to see if it looks like a valid internet hostname.
*/
function is_hostname($host)
{
//make sure $host: starts with a letter or digit.contains at least
//dot; only contains letters, digits, hyphens and gots; and ends with
//a TLD that is a at least 2 characters and only contains letters
if(!eregi("^[a-z0-9]{1}[a-z0-9\.\-]*\.[a-z]{2,}$",$host))
{
$this->errtype = DV_ERR_UNKNOWN;
$this->errstr = "网络地址不符合标准.您输入的是 ''$host'请检查";
return false;
}
//make sure the hostname dosen't contain any nonsense sequences
//involving adjacent dot/dashes
if (ereg("\.\.",$host) || ereg("\.-", $hos) || ereg("-\.", $host))
{
$this->errtype = DV_ERR_UNKNOWN;
$this->errstr = "网络地址中'.'或'_'的位置不正确.您输入的是 ''$host'";
return false;
}
return true;
}
/*
*method: is_email ($email)
*args: $email (value to test)
*note: tests $email to see if it looks like an e-mail address.
*/
function is_email($email)
{
$tmp = split("@",$email);
if (count($tmp) < 1)
return false;
if (count($tmp) > 2)
return false;
$username = $tmp[0];
$hostname = $tmp[1];
//make sure $usename component contains at least one legal
//character and no illegal ones.
if (!eregi("^[a-z0-9_\+\.\-]+$", $username))
return false;
//test hostname component using member method is_hostname()
if (!$this->is_hostname($hostname))
return false;
return true;
}
/*
*method: is_email_rfc822 ($email)
*args: $email (value to test)
*note: use imap_rfc822_parse_adrlist() and imap_errors() functions.
*/
/*function is_email_rfc822 ($email)
{
$addrs = imap_rfc822_parse_adrlist($email, '');
if (count ($addrs) != 1)
return false;
//$addr->mailbox 地址的"邮箱"部分
//$addr->host 地址的主机名部分
//$addr->personal 地址的"个人姓名"部分
//$addr->adl 域源路由
if (!$this->is_hostname ($addrs[0]->host))
return false;
if ($s = imap_errors())
$this->errstr = "发现以下错误:";
for ($i = 0; $i < count($s); $i++)
{
$this->errstr .= ($i + 1) . "." .$s[$i]."";
return false;
}
return true;
}*/
/*
*method: email_works ($email)
*args: $email (value to test)
*note: we simulate an SMTP session to the server and use the 'RCPT TO'
* to see whether the mail server will accept e-mail for this user.
*/
function email_works($email)
{
list ($addr, $hostname) = explode("@", $email);
if (!getmxrr($hostname, $mailhosts))
$mailhosts = array($mailhosts);
//if no mx records found simply try their hostname
$test_from = "hlxhxy@yahoo.com.cn";
for ($i = 0; $i < count($mailhosts); $i++)
{
//open a connection on port 25 to the recipient's mail server
$fp = fsockopen($mailhosts[$i], 25);
if (!$fp)
continue;
$r = fgets($fp, 1024);
//talk to the mail server
fwrite($fp, "HELLO" .$_SERVER['SERVER_NAME']."\r\n");
$r = fgets($fp, 1024);
if (!eregi("^250", $r))
{
fwrite($fp, "QUIT\r\n");
fclose($fp);
continue;
}
fwrite($fp, "MAIL FROM: <".$test_from.">\r\n");
$r = fgets($fp, 1024);
if (!eregi("^250", $r))
{
fwrite($fp, "QUIT\r\n");
fclose($fp);
continue;
}
fwrite($fp, "RCPT TO: <".$email.">\r\n");
$f = fgets($fp, 1024);
if (!eregi("^250", $r))
{
fwrite($fp,"QUIT\r\n");
fclose($fp);
continue;
}
//if we got here, we can send e-mail from $test_from to $email
fwrite($fp, "QUIT\r\n");
$r = fgets($fp, 1024);
fclose($fp);
return true;
}
}
/*
*method: is_cn_idcard ($cardnum)
*args: $cardnum
*note: tests $cardnum to see if it looks like an valid ID card number.
*/
function is_cn_idcard($cardnum)
{
$len = strlen($cardnum);
if (15 != $len && 18 != $len)
{
$this->errtype = DV_ERR_DIGIT_NOENOUGH;
$this->errstr = "身份证号码位数不正确.您输入的'$cardnum'是'$len'位.";
return false;
}
if(!preg_match("/(^\d{15}$)|(^\d{17}[\d|\*]{1}$)/i", $cardnum))
{
$this->errtype = DV_ERR_ILLEGAL_CHARS;
$this->errstr = "身份证号没能通过检验.您输入的是'$cardnum'";
return false;
}
return true;
}
}
?>
[/PHP]

