Home > Backend Development > PHP Tutorial > PHP regular expression to verify email address

PHP regular expression to verify email address

不言
Release: 2023-03-24 13:26:01
Original
2479 people have browsed it

这篇文章介绍的内容是关于PHP正则表达式 验证电子邮件地址,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

  我们最经常遇到的验证,就是电子邮件地址验证。网站上常见。各种网页脚本也都常用“正则表达式”(regular expression)对我们输入的电子邮件地址进行验证,判断是否合法。有的还能分解出用户名和域名。现在用PHP语言实现一下电子邮件地址验证程序,用的是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>
Copy after login

原文地址:
https://www.cnblogs.com/yejianfei/archive/2012/10/18/2729526.html

相关推荐 :

PHP正则匹配所有字符失败的原因及解决办法

  

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template