Home > Article > Backend Development > How to verify email address format using PHP regular expression
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:
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._-]+$/';
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,}$/';
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.
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,}$/';
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"; }
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!