How to verify email address format using PHP regular expression

WBOY
Release: 2023-06-24 11:32:02
Original
717 people have browsed it

With the widespread application of the Internet in our daily lives, email has become one of the important means for people to exchange information, and the correctness of the email address is particularly important. In web development, verifying whether the email address entered by the user is legitimate is a common task. PHP's regular expressions also make this task extremely simple.

Here are the steps to verify an email address in PHP:

  1. Regular expression for email address

In PHP, regular expression for verifying email address The formula is very complex and contains multiple groups and special characters. A basic email address format is: username@domain.com, where username is the username and domain is the domain name. Therefore, we can split the regular expression into two parts for matching.

Regular expression to verify username:

$pattern1 = '/^[a-zA-Z0-9._-]+$/';
Copy after login

This regular expression matches any length string consisting of uppercase and lowercase letters, numbers, dots, underscores, and minus signs. The above regular expression uses the two symbols ^ and $, which means that the match must start from the beginning of the string and end at the end.

Regular expression to verify domain names:

$pattern2 = '/^[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/';
Copy after login

This regular expression matches any length string consisting of uppercase and lowercase letters, numbers, dots, and minus signs, then a dot, and finally a An alphabetic string containing at least two characters. Among them, . is the escape character matching the dot.

  1. Regular expression combination of email address

Since the email address itself is composed of user name and domain name, we need to combine the above two regular expressions Combined together, complete verification of the email address can be completed. Therefore, the following regular expression can be obtained:

$pattern3 = '/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/';
Copy after login
  1. PHP code implementation

Through the above regular expression, we can use the preg_match() function in PHP, Verify the email address entered by the user:

$email = “example@email.com”;
$isValid = preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/', $email);
if($isValid) {
  echo "valid email address";
} else {
  echo "invalid email address";
}
Copy after login

In the above PHP code, $email is the email address we need to verify. The preg_match() function is used to match the regular expression and the email address. If the match is successful, it will be returned true, otherwise false is returned. When the email address format is correct, "valid email address" is output, otherwise "invalid email address" is output.

To sum up, PHP's regular expressions can easily verify whether the email address is legal, just follow the above steps to write it. In actual projects, the email address verification regular expression can be modified and expanded according to specific requirements.

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!