Home Backend Development PHP Tutorial PHP uses regular expressions to verify email addresses

PHP uses regular expressions to verify email addresses

Jul 16, 2017 am 09:50 AM
email php Mail

E-mail, like ordinary mail, also requires an address. The difference between it and ordinary mail is that it is an electronic address. All users with mailboxes on the Internet have one or several email addresses of their own, and these email addresses are all unique. The mail server sends each email to each user's mailbox based on these addresses. The Email address is the user's mailbox address. Just like regular mail, whether you can receive your email depends on whether you have obtained the correct email address. A complete Internet email address is composed of the following two parts. The format is as follows: login name@hostname.domain name. It is separated by a symbol "@" representing "at" (at) in the middle. The symbol The left side of is the other party's login name, and the right side is the complete host name, which consists of the host name and domain name. Among them, the domain name consists of several parts, each part is called a subdomain (Subdomain), and each subdomain is separated by a dot ".". Each subdomain will tell the user some information about this mail server.
php Regular matching email code

1. Verify email:

 < ?php 
   if (ereg("/^[a-z]([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/i; ",$email)){ 
echo “Your email address is correct!”;} 
   else{ 
echo "Please try again!"; 
} 
?>
Copy after login

or

$str = &#39;&#39;;
$isMatched = preg_match(&#39;/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/&#39;, $str, $matches);
var_dump($isMatched, $matches);
Copy after login

matches the

regular expression of the Email mailbox format Formula

:

Analysis:

/content/i constitutes a case-insensitive regular expression;

^ Match start
$ Match end
[a-z] The E-Mail prefix must start with an English letter

([a-z0-9]*[-_]?[a-z0-9]+)* and match _a_2, aaa11, _1_a_2, and a1_, aaff_33a_, and aaa do not match. If they are empty characters, they are also matched. * means 0 or more.
* represents 0 or more previous characters.
[a-z0-9]* matches 0 or more English letters or numbers
[-_]? matches 0 or 1 "- ", because "-" cannot appear continuously
[a-z0-9]+ matches one or more English letters or numbers, because "-" cannot be used as the end

@ There must be one @
([a-z0-9]*[-_]?[a-z0-9]+)+ see above ([a-z0-9]*[-_]?[a-z0-9 ]+)* explanation, but it cannot be empty, + means one or more.
[\.] Treat
special characters
(.) as ordinary characters [a-z]{2,3} to match 2 to 3 English letters, usually com or net, etc. ([\.][a-z]{2})? Matches 0 or 1 [\.][a-z]{2} (such as .cn, etc.) I don’t know if the last part of .com.cn is generally It is two digits. If not, please modify {2} to {number of starting words, number of ending words}

This regular expression used to match email addresses is relatively strong, powerful, wide-coverage, and useful. Save it for your friends.
The format of the international domain name is as follows:

The domain name is composed of any combination of the specific
character set
of each country's language, English letters, numbers and "-" (i.e. hyphen or minus sign). However, neither the beginning nor the end can contain "-", and "-" cannot appear continuously. Letters in domain names are not case-sensitive. The domain name can be up to 60 bytes long (including suffixes .com, .net, .org, etc.). /^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a- z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/i; /content/i forms a case-insensitive Regular expression;
^ Match start
$ Match end
[a-z] The E-Mail prefix must start with an English letter
([a-z0-9]*[-_]?[ a-z0-9]+)* matches _a_2, aaa11, _1_a_2, but does not match a1_, aaff_33a_, aaa. If it is a null character, it will also match. * means 0 or more.
* represents 0 or more previous characters.
[a-z0-9]* matches 0 or more English letters or numbers
[-_]? matches 0 or 1 "- ", because "-" cannot appear continuously
[a-z0-9]+ matches one or more English letters or numbers, because "-" cannot be used as the end
@ There must be @
([a-z0-9]*[-_]?[a-z0-9]+)+ see above ([a-z0-9]*[-_]?[a-z0-9]+) *Explanation, but it cannot be empty, + means one or more.
[\.] Treat special characters (.) as ordinary characters
[a-z]{2,3} matches 2 to 3 English letters, usually com or net, etc.
([\. ][a-z]{2})? Matches 0 or 1 [\.][a-z]{2} (such as .cn, etc.) I don’t know if the last part of .com.cn is usually two digits. If not, please modify {2} to {number of starting words, number of ending words}
Perfect E-Mail regular expression, with detailed explanation, please help test it! 2. Extract the email in
String
:

Run as follows:

Array 
( 
[0] => 9999@qq.com.cn 
[1] => fuyongjie@163.com 
[2] => hh@qq.com 
[3] => fuyongjie.100@yahoo.com 
[4] => fu-1999@sina.com 
)
Copy after login

3. Compare: The regular expression in the second one does not contain the first one. ^and$;

The above is the detailed content of PHP uses regular expressions to verify email addresses. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
Download the official website of Ouyi Exchange app for Apple mobile phone Download the official website of Ouyi Exchange app for Apple mobile phone Apr 28, 2025 pm 06:57 PM

The Ouyi Exchange app supports downloading of Apple mobile phones, visit the official website, click the "Apple Mobile" option, obtain and install it in the App Store, register or log in to conduct cryptocurrency trading.

Sesame Open Door Official Website Entrance Sesame Open Door Official Latest Entrance 2025 Sesame Open Door Official Website Entrance Sesame Open Door Official Latest Entrance 2025 Apr 28, 2025 pm 07:51 PM

Sesame Open Door is a platform that focuses on cryptocurrency trading. Users can obtain portals through official websites or social media to ensure that the authenticity of SSL certificates and website content is verified during access.

Binance official website entrance Binance official latest entrance 2025 Binance official website entrance Binance official latest entrance 2025 Apr 28, 2025 pm 07:54 PM

Visit Binance official website and check HTTPS and green lock logos to avoid phishing websites, and official applications can also be accessed safely.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

How to download, install and register the Hong Kong Digital Currency Exchange app 2025 How to download, install and register the Hong Kong Digital Currency Exchange app 2025 Apr 30, 2025 pm 07:18 PM

The download, installation and registration process of the Hong Kong Digital Currency Exchange app is very simple. Users can quickly obtain and use this app through the official app download link provided in this article. This article will introduce in detail how to download, install and register the Hong Kong Digital Currency Exchange app to ensure that every user can complete the operation smoothly.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

How to configure the character set and collation rules of MySQL How to configure the character set and collation rules of MySQL Apr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

See all articles