Home > Backend Development > PHP Tutorial > PHP regular expression to verify the validity of email domain name

PHP regular expression to verify the validity of email domain name

WBOY
Release: 2023-06-24 11:22:02
Original
1333 people have browsed it

With the development and popularization of the Internet, email has become an indispensable part of people's daily work and life. However, in order to ensure the validity and security of the email domain name, we need to perform certain verification on it. This article will use PHP regular expressions as the basis to introduce how to verify the validity of the email domain name.

  1. The composition of the email domain name

Before verifying the validity of the email domain name, we need to know the composition of the email domain name. Generally speaking, an email consists of two parts: the email name and the domain name. The email name is separated by the "@" symbol, and the domain name is composed of the part after the "@" symbol. For example: in example@mail.com, the email address is example and the domain name is mail.com.

  1. How to verify the validity of the email domain name

In PHP, we can use regular expressions to verify the email domain name. The following is a basic regular expression example for verifying email domain names:

if(preg_match('/^(([a-zA-Z0-9_-])+.)*[a-zA-Z0-9_-]+.([a-zA-Z]{2,6})$/', $email_domain)) {
    // 邮箱域名格式正确
} else {
    // 邮箱域名格式错误
}
Copy after login

In the above regular expression, the part included by the "^" and "$" qualifiers indicates that all the requirements of the regular expression must be met. , otherwise the verification fails. Specifically, the regular expression is parsed as follows:

  • "^" represents the beginning of the regular expression;
  • "(([a-zA-Z0-9_-]) .)" represents the front part of the domain name (can be composed of multiple letters, numbers, underscores or dashes, each part is separated by "."), where "" means that the front part can appear 0 or multiple times;
  • "[a-zA-Z0-9_-]" represents the last part of the domain name (composed of letters, numbers, underscores or underscores);
  • ".( [a-zA-Z]{2,6})" represents the suffix of the domain name (such as .com, .cn, etc.), where "." represents the "." symbol, and "[a-zA-Z]{2 ,6}" means that the domain name suffix must be composed of 2-6 letters (both upper and lower case);

According to the above analysis, we can find that this regular expression can verify domain names that meet the following requirements:

  • The domain name consists of multiple letters, numbers, underscores or dashes, and the middle separator is ".";
  • The last part of the domain name consists of 2-6 letters, is a legal domain name suffix.
  1. Implementation of verifying email validity

Based on the above regular expression, we can write a PHP function to verify the validity of the email domain name. The specific implementation code is as follows:

function validate_email_domain($email) {
    $email_parts = explode('@', $email);
    $email_domain = $email_parts[1];
    if(preg_match('/^(([a-zA-Z0-9_-])+.)*[a-zA-Z0-9_-]+.([a-zA-Z]{2,6})$/', $email_domain)) {
        return true;
    } else {
        return false;
    }
}
Copy after login

The implementation of this function includes the following two steps:

  • First, use the "explode()" function to separate the email name and domain name parts;
  • Then, call the above regular expression to verify the email domain name. If the verification is successful, it will return true, otherwise it will return false.
  1. Summary

Through the introduction of this article, we have learned how to verify the validity of email domain names based on PHP regular expressions. By writing the above function, we can easily implement the validity check of the email domain name in the PHP project, avoiding registration, login and other problems caused by illegal domain names. At the same time, readers can also apply the method in this article to projects in other languages ​​to verify the validity of email domain names.

The above is the detailed content of PHP regular expression to verify the validity of email domain name. 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