php 邮件验证_PHP程序来验证电子邮件地址

php 邮件验证

Suppose there is a form floating where every user has to fill his/her email ID. It might happen that due to typing error or any other problem user doesn't fill his/her mail ID correctly. Then at that point, the program should be such that it should print a user-friendly message notifying the user that address filled is wrong. This is a simple program and can be done in two ways in PHP language.

假设有一个浮动的表单,每个用户都必须填写他/她的电子邮件ID。 由于键入错误或其他任何问题,用户可能无法正确填写其邮件ID。 然后,在那时,程序应该是这样的:它应该打印一条用户友好的消息,通知用户地址填写错误。 这是一个简单的程序,可以用PHP语言以两种方式完成。

Method 1: Naive approach

方法1:天真的方法

There is a filter called FILTER_VALIDATE_EMAIL which is in-built in PHP and validates mail ID.

有一个名为FILTER_VALIDATE_EMAIL的过滤器,该过滤器是PHP内置的,可验证邮件ID。

The function filter_var() is also used in this program which takes two arguments. The first is the user mail ID and the second is the email filter. The function will return a Boolean answer according to which we can print the message of our desire.

此程序中也使用了filter_var()函数,该函数接受两个参数。 第一个是用户邮件ID,第二个是电子邮件过滤器。 该函数将返回一个布尔值答案,根据该答案我们可以打印所需的消息。

Program:

程序:

<?php

    $email = "[email protected]";
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
         echo '"' . $email . ' " is valid'."\n";
    }
    else {
         echo '"' . $email . ' " is Invalid'."\n";
    }

    $email = "inf at includehelp.com";
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
         echo '"' . $email . ' " is valid'."\n";
    }
    else {
         echo '"' . $email . ' " is Invalid'."\n";
    }
    
?>

Output:

输出:

"[email protected] " is valid
"inf at includehelp.com " is Invalid

Method 2: Separating strings

方法2:分隔字符串

How a normal human being validates some email addresses? The human observes some pattern in the string as well as some special characters while checking the validation of an email. The same can be done through programming. An email ID should necessarily have the character '@' and a string '.com' in a specific order. A function called preg_match() will be used for checking this order and characters.

普通人如何验证某些电子邮件地址? 人们在检查电子邮件的有效性时会观察到字符串中的某些模式以及一些特殊字符。 可以通过编程完成相同的操作。 电子邮件ID必须按特定顺序包含字符“ @”和字符串“ .com” 。 称为preg_match()的函数将用于检查此顺序和字符。

<?php
    // A functios is created for checking validity of mail
    function mail_validation($str) {
        return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
    }
    
    // Taking user input of email ID
    $a=readline('Input an email address: ');
    if(!mail_validation($a))
    {
        echo "Invalid email address.";
    }
    else{
        echo "Valid email address.";
    }
?>

Output:

输出:

RUN 1:
Input an email address: [email protected]
Valid email address.

RUN 2:
Input an email address: [email protected]
Invalid email address.


翻译自: https://www.includehelp.com/php/program-to-validate-an-email.aspx

php 邮件验证

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值