Home > Backend Development > PHP Tutorial > How to verify email format using php regular expression

How to verify email format using php regular expression

PHPz
Release: 2023-06-24 14:12:02
Original
1797 people have browsed it

With the development of the Internet, email has become an indispensable part of people's daily life and work. In website development, it is often necessary to verify whether the email entered by the user meets the specifications. This article will introduce using PHP regular expressions to verify email format.

  1. Rules of email format

Before verifying the email format, let’s first understand the rules of email format. Generally speaking, a legal email address should contain the following parts:

① Username, usually a combination of lowercase letters, numbers and special characters, and can include dots (.) and dashes (-) ;

②@ symbol, separates user name and domain name;

③Domain name, including domain name suffix and domain name prefix, such as com.cn, net, etc.

  1. PHP Regular Expression

PHP uses regular expressions to match strings that match certain rules. Regular expressions are composed of patterns and tags. Patterns refer to matching rules, and tags are symbols used to modify patterns.

Example:

$pattern = '/regular expression/';

Among them, / is the delimiter, and the regular expression is written between the two delimiters between. There are several types of tags:

i: case-insensitive

m: multi-line matching pattern

g: global matching pattern

  1. PHP Verify Email Format

The following is the code that uses PHP regular expressions to verify the email format:

$email = "example@mail.com" ; // Test email address
$pattern = '/^[a-z0-9] ([._-][a-z0-9] )*@[a-z0-9] (.[a-z] ){1,2}$/i'; // Regular expression

if (preg_match($pattern, $email)) {

9b70a95e30fd4e369b607da010fcdc38

}
?>

In the above code, we use the preg_match function to match whether the email address meets the specification. Among them, $pattern is the regular expression we defined, which can be modified according to needs.

  1. Detailed explanation of regular expressions

The general principle of the above regular expression $/i is as follows:

/^ starts with a string

[a-z0-9] matches a combination of lowercase letters and numbers, indicating at least one match

(._- ) matches. _ - symbol followed by a combination of lowercase letters and numbers, means it can be matched zero or more times

@ matches the @ symbol

[a-z0-9] matches a combination of lowercase letters and numbers, which means it matches at least once

( .[a-z] ){1,2} matches. The symbol is followed by a lowercase letter, {1,2} indicates that the set of matches can be repeated once or twice

$/i ends with a string, i indicates the size Write indifferently

The above regular expression can match most email addresses, but there are some special cases that require additional processing, such as those containing Chinese characters.

  1. Summary

Using PHP regular expressions to verify the email format can help us quickly and accurately determine whether the email entered by the user is correct and improve the security and stability of the website. . At the same time, we should also note that the design of regular expression rules needs to be combined with specific business scenarios to ensure that they can meet usage needs.

The above is the detailed content of How to verify email 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