How to verify email address with PHP regular expression

PHPz
Release: 2023-06-24 10:18:01
Original
3038 people have browsed it

How to verify email address with PHP regular expression

With the advent of the Internet era, email has become an indispensable communication tool in our daily life and work. However, when using email for communication, how to ensure that the entered email address is in the correct format?

As a widely used scripting language, PHP provides a set of regular expression function libraries that can verify and process various texts, including email addresses. In this article, we will introduce how to verify email addresses using PHP regular expressions.

1. Regular expression verification of email addresses

When using PHP to verify email addresses with regular expressions, we need to use a specific regular expression pattern. The following is a basic regular expression pattern:

/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/ 
Copy after login

The meaning of this regular expression pattern is:

  1. The email address consists of three parts: post office user name, @ symbol, Post office domain name;
  2. The post office user name can contain letters, numbers, dots, underscores, percent signs, plus signs, minus signs and other characters, with no length limit;
  3. @ symbol connects the post office user name and Post office domain name;
  4. Post office domain name can contain letters, numbers, dots, minus signs and other characters, with no limit on length;
  5. The last dot must be followed by letters, and the length is 2 to 6 characters.

2. PHP code example

It is not difficult to use PHP to verify email addresses. Here is a sample code:

function is_valid_email_address($email) {
    $pattern = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/';
    return preg_match($pattern, $email);
}

$email1 = 'example@domain.com';
$email2 = 'example@domain.co.uk';

if (is_valid_email_address($email1)) {
    echo $email1 . ' is a valid email address.';
} else {
    echo $email1 . ' is not a valid email address.';
}

if (is_valid_email_address($email2)) {
    echo $email2 . ' is a valid email address.';
} else {
    echo $email2 . ' is not a valid email address.';
}
Copy after login

The running results are as follows:

example@domain.com is a valid email address.
example@domain.co.uk is a valid email address.
Copy after login

3. Conclusion

This article introduces the method of using PHP regular expressions to verify email addresses, and gives a practical PHP code example. As a PHP developer, learning to use regular expressions to validate various text inputs is one of the essential skills. I hope this article can be helpful to everyone.

The above is the detailed content of How to verify email address with 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!