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

藏色散人
Release: 2019-11-09 18:01:48
Original
9765 people have browsed it

This article mainly introduces to you two implementation methods ofPHP 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.


        
Copy after login

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('244'));
Copy after login

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('244@qq.com'));
Copy after login

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


        
Copy after login

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('244@qq.com'));
Copy after login

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('244'));
Copy after login
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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!