PHP头条
热点:

清单5:校验本地部分内容的局部测试

if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                str_replace("\\\\","",$local)))
{
      if (!preg_match('/^"(\\\\"|[^"])+"$/', 
                   str_replace("\\\\","",$local)))
   {
      $isValid = false;
   }
}

这里的外部测试代码中正则表达式看起来就是一串被接受的字符序列或转移字符,内部测试代码实际上就是一串引用的转移字符或由一对引号括起来的其他字符。

如果你正在校验一个进入POST数据的email地址,你需要注意斜线\)、单引号')和双引号")字符,PHP可能会也可能不会转义这些字符,对应这个行为的是magic_quotes_gpc,gpc意味着get,post,cookie。在你的代码中,可以调用get_magic_quotes_gpc()函数,在正面响应中将额外的斜线剥离出去,你也可以在PHP.ini文件中将这台特性禁用掉,另两个参数是magic_quotes_runtime和magic_quotes_sybase。

清单5中的两个正则表达式看起来还是非常诱人的,因为它相对要容易理解一些,并且不需要重复接受的字符组[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-]。为什么在一个斜线前面需要两个反斜线字符,以及在单引号之前为什么要一个反斜线?

清单5中外部测试的一个缺点是在传递本地字符串时,包括了所有的点字符.),必要条件2已经说明了本地字符串不能以点开头,也不能以点结尾,而且不能同时连续出现两个或更多的点。我们可以将外部正则表达式修改为^(a+(\.a+)+)$,这里的a等于\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~-],但这样会导致表达式较长,难以理解,添加如清单6显示的检查就变得清晰了。

清单6:检查本地部分点的位置

if ($local[0] == '.' || $local[$localLen-1] == '.')
{
   // local part starts or ends with '.'
   $isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
   // local part has two consecutive dots
   $isValid = false;
}

将本地部分看作一个包,现在要检查本地部分所有的必要条件,检查域后将完成email地址的校验,代码将会独立地检查域的每一个标记,与清单2中的代码一样,但这个解决办法代表这里允许DNS检查,检查域是否有效。

清单7做了一个粗略的检查,确保域部分只包括可用的字符,无重复的点,然后继续查询DNS A记录和MX记录。仅当MX记录检查失败时,才会检查A记录,清单4中的代码校验的是域部分长度。

清单7:域检查

if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
   // character not valid in domain part
   $isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
   // domain part has two consecutive dots
   $isValid = false;
}
else if (!(checkdnsrr($domain,"MX") || checkdnsrr($domain, "A")))
{
   // domain not found in DNS
   $isValid = false;
}

这样就好了吗?最好还是测试一下它的逻辑,看是否正确,清单8包括了一系列的email地址测试用例。

清单8:email有效性测试函数

<?php
require("validEmail.php"); // your favorite here

function testEmail($email)
{
  echo $email;
  $pass = validEmail($email);
  if ($pass)
  {
    echo " is valid.\n";
  }
  else
  {
    echo " is not valid.\n";
  }
  return $pass;
}

$pass = true;
echo "All of these should succeed:\n";
$pass &= testEmail("dclo@us.ibm.com");
$pass &= testEmail("abc\\@def@example.com");
$pass &= testEmail("abc\\\\@example.com");
$pass &= testEmail("Fred\\ Bloggs@example.com");
$pass &= testEmail("Joe.\\\\Blow@example.com");
$pass &= testEmail("\"Abc@def\"@example.com");
$pass &= testEmail("\"Fred Bloggs\"@example.com");
$pass &= testEmail("customer/department=shipping@example.com");
$pass &= testEmail("\$A12345@example.com");
$pass &= testEmail("!def!xyz%abc@example.com");
$pass &= testEmail("_somename@example.com");
$pass &= testEmail("user+mailbox@example.com");
$pass &= testEmail("peter.piper@example.com");
$pass &= testEmail("Doug\\ \\\"Ace\\\"\\ Lovell@example.com");
$pass &= testEmail("\"Doug \\\"Ace\\\" L.\"@example.com");
echo "\nAll of these should fail:\n";
$pass &= !testEmail("abc@def@example.com");
$pass &= !testEmail("abc\\\\@def@example.com");
$pass &= !testEmail("abc\\@example.com");
$pass &= !testEmail("@example.com");
$pass &= !testEmail("doug@");
$pass &= !testEmail("\"qu@example.com");
$pass &= !testEmail("ote\"@example.com");
$pass &= !testEmail(".dot@example.com");
$pass &= !testEmail("dot.@example.com");
$pass &= !testEmail("two..dot@example.com");
$pass &= !testEmail("\"Doug \"Ace\" L.\"@example.com");
$pass &= !testEmail("Doug\\ \\\"Ace\\\"\\ L\\.@example.com");
$pass &= !testEmail("hello world@example.com");
$pass &= !testEmail("gatsby@f.sc.ot.t.f.i.tzg.era.l.d.");
echo "\nThe email validation ";
if ($pass)
{
   echo "passes all tests.\n";
}
else
{
   echo "is deficient.\n";
}
?>

请务必进行测试,查看email地址是否合法,PHP脚本中的双斜线\\)转义字符很容易混淆,你编写的email地址校验代码将接受这个测试代码的挑战,确保清单9中的代码测试通过。


www.phpzy.comtrue/php/19078.htmlTechArticle清单5:校验本地部分内容的局部测试 if (!preg_match('/^(\\\\.|[A-Za-z0-9!#% }} 这里的外部测试代码中正则表达式看起来就是一串被接受的字符序列或转移字符,内...

相关文章

PHP之友评论

今天推荐