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'));
The browser access results are as follows:
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'));
The result is:
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'));
The results are as follows:
##As shown in the figure, when we enter the correct email format, the return value is 1;
var_dump(checkEmail('244'));
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 If you want to learn more about PHP, you can also follow the PHP Chinese websitePHP 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!