Home  >  Article  >  Daily Programming  >  How to verify whether the email format is correct with PHP? (Pictures + Videos)

How to verify whether the email format is correct with PHP? (Pictures + Videos)

藏色散人
藏色散人Original
2018-09-26 14:09:319856browse

This article mainly introduces to you two implementation methods of PHP verification email format.

During the website development process, some websites may need to implement user registration functions. Then user registration can be a mobile phone number or email. Registration through email often requires checking and verifying the email format to prevent the website from being maliciously registered.

Below we will introduce two methods for PHP to determine the email format through simple code examples.

Method 1: Through the eregi function

eregi: Case-insensitive regular expression matching.

<?php
function validateEmail($email)
{
    // eregi 从PHP5往后去就被废弃了
    if (@eregi(&#39;^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$&#39;, $email)) {
        return true;
    } else {
        return false;
    }
}

Here we define a validateEmail method, which uses eregi to match whether the first parameter and the second parameter are the same to determine whether the email format is correct.

The first parameter in eregi() represents the defined mailbox regular expression, and the second parameter is the string we entered, which is the mailbox.

We can call this method and enter the following content for testing:

var_dump(validateEmail(&#39;244&#39;));

The browser access results are as follows:

How to verify whether the email format is correct with PHP? (Pictures + Videos)

such as The return value in the figure is false, indicating that the email format is incorrect.

Then enter the following content to test:

var_dump(validateEmail(&#39;244@qq.com&#39;));

The result is:

How to verify whether the email format is correct with PHP? (Pictures + Videos)

As shown in the figure, the return value is true, which means it is the correct email address Format.

Note: eregi can only be used under PHP4 and PHP5.

Method 2: Through the preg_match function

preg_match: execute matching regular expression

<?php
function checkEmail($email)
{
    $pregEmail = "/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i";
    return preg_match($pregEmail, $email);
}

Similarly, use The preg_match function matches two parameters to determine whether the email format is correct.

We can also test it here:

var_dump(checkEmail(&#39;244@qq.com&#39;));

The results are as follows:

How to verify whether the email format is correct with PHP? (Pictures + Videos)

##As shown in the figure, when we enter the correct email format, the return value is 1;

var_dump(checkEmail(&#39;244&#39;));

When the email format does not match, the return value is 0, as shown below:

How to verify whether the email format is correct with PHP? (Pictures + Videos)

Note: preg_match can be used under PHP 4, PHP 5, PHP 7.

This article is an introduction to the two methods of verifying the email format in PHP. For the knowledge of regular expressions involved, you can refer to the PHP Chinese website

Regular Expression Manual

If you want to learn more about PHP, you can also follow the PHP Chinese website

PHP Video Tutorial, everyone is welcome to learn and refer to it!

The above is the detailed content of How to verify whether the email format is correct with PHP? (Pictures + Videos). 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