Home > Backend Development > PHP Tutorial > How to use PHP regular expressions to verify whether the input string is in the correct QQ number or email format

How to use PHP regular expressions to verify whether the input string is in the correct QQ number or email format

PHPz
Release: 2023-06-24 11:16:01
Original
2933 people have browsed it

PHP Regular expressions are a technique widely used for string matching and search. When developing web applications, it is often necessary to verify user input data to ensure that the input data format is correct, safe and legal. This article will introduce how to use PHP regular expressions to verify whether the input string is in the correct QQ number or email format.

1. Verify QQ number format

QQ number is a unique ID for users to chat and communicate online on the Internet. QQ numbers are generally 5-15 digits, and the first digit cannot be 0. In order to verify whether the input string is in the correct QQ number format, we can use PHP regular expressions.

The following is a sample code:

$qq = "123456"; //待验证的QQ号码
$pattern = '/^[1-9][0-9]{4,14}$/'; //QQ号码正则表达式
if (preg_match($pattern, $qq)) {
  echo "QQ号码格式正确!";
} else {
  echo "QQ号码格式不正确!";
}
Copy after login

In the above code, we first define a QQ number $qq to be verified, and then define a regular expression $pattern for the QQ number . The meaning of the regular expression is:

  • ^: Indicates the beginning of the string
  • [1-9]: Indicates that the first digit cannot be 0, and the value range is 1-9 Numbers between
  • [0-9]{4,14}: Indicates the 4-14 digits in the middle of the QQ number
  • $: Indicates the end of the string

If the entered QQ number complies with the rules of the regular expression, that is, it is the correct QQ number format, then the preg_match function returns a value of 1, otherwise the return value is 0. Determine whether the QQ number is correct based on the return value.

2. Verify email format

Mailbox is an email communication method used to send and receive emails on the Internet. The format of the mailbox is generally username@domain.com, where username represents the user name, domain represents the domain name, and com represents the type of mail server. In order to verify whether the input string is in the correct email format, we can also use PHP regular expressions.

The following is a sample code:

$email = "test@qq.com"; //待验证的邮箱地址
$pattern = '/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/'; //邮箱正则表达式
if (preg_match($pattern, $email)) {
  echo "邮箱格式正确!";
} else {
  echo "邮箱格式不正确!";
}
Copy after login

In the above code, we first define an email address to be verified $email, and then define a regular expression $pattern for the email. The meaning of the regular expression is:

  • ^: represents the beginning of the string
  • w: represents the user name, which can contain letters, numbers and underscores
  • ([ - .]w )*: Indicates other characters that can be included in the user name, such as ., -,
  • @: Indicates the separator in the email address @
  • w: Indicates the domain name, which can be Contains letters, numbers and underscores
  • ([-.]w )*: Indicates other characters that can be included in the domain name, such as., -
  • .: Indicates the delimiter of the mail server type.
  • w ([-.]w )*: Indicates the mail server type, which can contain letters, numbers, - and .
  • $: Indicates the end of the string

If the input email conforms to the rules of the regular expression, that is, it is the correct email format, then the preg_match function returns a value of 1, otherwise the return value is 0. Determine whether the email format is correct based on the return value.

Summary

This article introduces how to use PHP regular expressions to verify whether the input string is in the correct QQ number or email format. By using regular expressions, we can easily perform format validation and filtering of user input data, thereby improving the security and reliability of web applications. In the actual development process, we can also apply regular expressions to verify other common user input data formats, such as mobile phone numbers, ID numbers, etc.

The above is the detailed content of How to use PHP regular expressions to verify whether the input string is in the correct QQ number or email format. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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