Home  >  Article  >  Backend Development  >  PHP regular expression in action: matching email addresses

PHP regular expression in action: matching email addresses

WBOY
WBOYOriginal
2023-06-22 12:41:531536browse

In the modern Internet era, email has become one of the important ways for people to communicate. In network application development, it is often necessary to verify email addresses to ensure their validity and legality. Regular expressions are a powerful tool for this kind of task.

In PHP, by using the regular expression function preg_match(), we can easily verify email addresses. The following is a simple example:

$email = "example@example.com";

if (preg_match("/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/", $email)) {
  echo "Valid email address.";
} else {
  echo "Invalid email address.";
}

In the above code, use the regular expression "/^w ([- .]w )@w ([-.]w ).w ([-.]w )*$/" matched the email address. The meaning of this regular expression is to match strings that meet the following conditions:

  • starts with one or more letters, numbers or underscores, and can be followed by one or more "-", ". ” or “ ” symbol, followed by one or more letters, numbers, or underscores;
  • is followed by an “@” symbol; the part after
  • is one or more letters, numbers, or Underscore, which can be followed by one or more "-" or "." symbols, followed by one or more letters, numbers or underscores; the last part of
  • is a "." symbol and one or more Letters, numbers, or underscores, followed by one or more "-" or "." symbols, followed by one or more letters, numbers, or underscores.

If the match is successful, it means the email address is valid and "Valid email address." is output; otherwise, it means it is illegal and "Invalid email address." is output.

In addition to using this relatively simple regular expression, we can also use some more complex regular expressions to further effectively determine the legitimacy of the email address. For example, we can use the following regular expression:

$email = "example@example.com";

if (preg_match("/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z|a-z]{2,}/", $email)) {
  echo "Valid email address.";
} else {
  echo "Invalid email address.";
}

The meaning of this regular expression can be briefly described as follows:

  • Starts with a word boundary;
  • Match one or more letters, numbers, underscores, dots, percent signs, plus signs, minus signs or the symbol "@";
  • then matches the "@" symbol;
  • matches after that One or more letters, numbers, dots or minus signs, where dots and minus signs cannot start or end;
  • ends with a dot symbol and two or more lowercase letters or uppercase letters.

This regular expression is more complete and precise, can effectively determine the legitimacy of email addresses, and covers more marginal situations.

In short, using regular expressions to verify email addresses in PHP is a very important task, which can effectively ensure the security and stability of network applications. Whether you are developing websites, e-commerce, social media or mobile apps, regular expressions are so necessary and useful.

The above is the detailed content of PHP regular expression in action: matching email addresses. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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