Home  >  Article  >  Backend Development  >  PHP regular expression library implements email address verification

PHP regular expression library implements email address verification

墨辰丷
墨辰丷Original
2018-06-06 10:04:041044browse

本篇文章主要介绍PHP正则表达式库实现邮件地址验证,感兴趣的朋友参考下,希望对大家有所帮助。

代码如下:

<?php
  header ( "Content-Type: text/html; charset=UTF-8" );
  $reply = "";
  if ( isset($_POST["email_address"]) )
  {
    $email_address = $_POST["email_address"];
    $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
    if ( preg_match( $pattern, $email_address ) )
    {
      $reply = "您输入的电子邮件地址合法<br /><br />\n";
      $user_name = preg_replace( $pattern ,"$1", $email_address );
      $domain_name = preg_replace( $pattern ,"$2", $email_address );
      $reply .= "用户名:".$user_name."<br />\n";
      $reply .= "域名:".$domain_name."<br />\n\n";
    }
    else
    {
      $reply = "您输入的电子邮件地址不合法";
    }
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh">
<head>
<title>电子邮件地址验证程序</title>
</head>
<body style="text-align: center;">
<h1>电子邮件地址验证程序</h1>
<form action="#" method="post">
请输入电子邮件地址:<input name="email_address" type="text" style="width: 300px;" /><br />
<input type="submit" value="验证电子邮件地址" />
</form>
<?php
  echo $reply;
?>
</body>
</html>

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP判断手机是IOS还是Android的三个方法

php观察者模式详解

PHP基于邮件类发送邮件的方法

The above is the detailed content of PHP regular expression library implements email address verification. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn