Home Backend Development PHP Tutorial Regular expression to match email address in php_PHP tutorial

Regular expression to match email address in php_PHP tutorial

Jul 13, 2016 am 10:44 AM
a php match address Example replace regular use expression Mail

An example of a regular expression for matching email addresses in PHP. The regular matching expression I commonly use when replacing email addresses: /^[a-z]([a-z0-9]*[-_]?[a-z0 -9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z ]{2})?$/i, the following is a detailed analysis for friends in need.

php example

The following uses PHP as an example:

 代码如下 复制代码
< ?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!”;
}
?>

Description:
①/content/i forms a case-insensitive regular expression; ^ starts the match; $ ends the match.

②[a-z] 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_, a__aa, if it is a null character , is also matched, * means 0 or more.

④* represents 0 or more preceding characters.

⑤[a-z0-9]* matches 0 or more English letters or numbers; [-_]? matches 0 or 1 "-", because "-" cannot appear continuously.

⑥[a-z0-9]+ matches 1 or more English letters or numbers, because "-" cannot be used as the end

⑦@ There must be someone @

⑧([a-z0-9]*[-_]?[a-z0-9]+)+ See above ([a-z0-9]*[-_]?[a-z0-9 ]+)* explanation, but it cannot be empty, + means one or more.

⑨[.] treats 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 bit, if not, please change {2} to {number of starting words, number of ending words}


js example

The code is as follows
 代码如下 复制代码

<script><br> function Email(ee){<br> var emailreg = "^w+@w+.w+(.w+)*$";</p> <p>var rege = new RegExp(emailreg, 'g');<br> alert(rege.test(ee));<br> }<br> var ee1 = "12xwz@123e^rsrf6.csdfdfom.df";<br> var ee2 = "12xwz@123ersrf6.csdfdfom.df";<br> Email(ee1);<br> Email(ee2);<br> </script>

Copy code



<script><br> function Email(ee){<br> var emailreg = "^w+@w+.w+(.w+)*$";<br> <br>var rege = new RegExp(emailreg, 'g');<br> alert(rege.test(ee)); }</td> var ee1 = "12xwz@123e^rsrf6.csdfdfom.df";</tr> var ee2 = "12xwz@123ersrf6.csdfdfom.df";</table> Email(ee1); Email(ee2);<p align="left"> </script>

In order for you to better master regular expressions, please learn the string escape form Here, you use a string to represent the regular expression, and the escape character must be expressed. If you want to match, use ^w+@w+.w+(.w+)*$ It starts with w word and has one or more @后 It’s a word .followed by a word with one or more (.w+)* can have none or multiple
http://www.bkjia.com/PHPjc/633085.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633085.htmlTechArticleAn example of a regular expression for matching email addresses in PHP. Regular expressions for replacing regular email addresses. My commonly used regular matching expressions: /^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9 ]+)+[\.][a-z...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Top 10 virtual currency trading app platforms 2025 Top 10 virtual currency trading app platforms latest list Top 10 virtual currency trading app platforms 2025 Top 10 virtual currency trading app platforms latest list Mar 06, 2025 pm 04:21 PM

The top ten virtual currency trading app platforms in 2025 are: 1. OKX; 2. Binance; 3. Gate.io; 4. Huobi Global; 5. Kraken; 6. Coinbase; 7. KuCoin; 8. Crypto.com; 9. Bitfinex; 10. MEXC Global. The ranking considers factors such as market share, user experience, security, transaction fees, etc., but is for reference only.

okx Ouyi official website registration portal 2025 okx Ouyi official website registration portal 2025 Mar 04, 2025 pm 11:45 PM

OKX Ouyi is a leading cryptocurrency trading platform. This article will provide detailed steps to guide you on how to register an OKX Ouyi official website account. You will learn how to access the official website, choose the registration method, fill in the necessary information, and complete the registration process. The article also contains information about precautions, such as the importance of using real personal information and setting a strong password.

Sesame Open Door Exchange Web Page Login Latest version gateio official website entrance Sesame Open Door Exchange Web Page Login Latest version gateio official website entrance Mar 04, 2025 pm 11:48 PM

A detailed introduction to the login operation of the Sesame Open Exchange web version, including login steps and password recovery process. It also provides solutions to common problems such as login failure, unable to open the page, and unable to receive verification codes to help you log in to the platform smoothly.

gateio exchange app old version gateio exchange app old version download channel gateio exchange app old version gateio exchange app old version download channel Mar 04, 2025 pm 11:36 PM

Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.

How to register and download the latest app on Bitget official website How to register and download the latest app on Bitget official website Mar 05, 2025 am 07:54 AM

This guide provides detailed download and installation steps for the official Bitget Exchange app, suitable for Android and iOS systems. The guide integrates information from multiple authoritative sources, including the official website, the App Store, and Google Play, and emphasizes considerations during download and account management. Users can download the app from official channels, including app store, official website APK download and official website jump, and complete registration, identity verification and security settings. In addition, the guide covers frequently asked questions and considerations, such as

Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Mar 04, 2025 pm 04:51 PM

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.

gate.io official login web version Sesame Open Door Trading Platform web version login web version gate.io official login web version Sesame Open Door Trading Platform web version login web version Mar 04, 2025 pm 04:33 PM

The login method of the Sesame Open Door Trading Platform is convenient. Users only need to visit their official website (please search for the domain name yourself), enter the registered email/mobile phone number and password to log in. The platform may enable security verification mechanisms such as 2FA to ensure account security.

Ouyi okx trading platform app official website registration portal Ouyi okx trading platform app official website registration portal Mar 04, 2025 pm 11:27 PM

OKX trading platform, a digital asset trading platform trusted by global users. The platform provides a variety of digital asset trading services, including spot trading, futures trading, option trading, etc.

See all articles