Home  >  Article  >  Backend Development  >  How to verify username format with PHP regular expression

How to verify username format with PHP regular expression

PHPz
PHPzOriginal
2023-06-25 08:06:291522browse

In web applications, verifying usernames is a very important part. When users register, you need to ensure that the username meets specific rules to prevent users from entering invalid or malicious usernames. PHP provides a powerful and flexible regular expression (Regex for short) function that can be used to check and verify input text. This article will introduce how to use PHP's regular expressions to verify username format.

Verify username rules

Before writing the username verification regular expression, you first need to determine the username rules. Normally, usernames should only contain letters, numbers, and underscores. It should be between 4 and 16 characters in length, and cannot start with a number or an underscore, or end with an underscore. In addition, in order to prevent users from entering sensitive information or attack code, the username should filter out some special characters, such as angle brackets, quotation marks, slashes, etc.

The following is an example username rule:

  • Contains only letters, numbers and underscores
  • Length between 4~16 characters
  • Cannot start with numbers and underscores
  • Cannot end with underscores
  • Filter out special characters

Write regular expressions

With rules , we can write regular expressions. In PHP, use the preg_match() function to check whether user input conforms to regular expression rules. The following is a basic regular expression for validating usernames:

$pattern = '/^[a-zA-Z0-9_]{4,16}$/';

this The regular expression uses the locators ^ (matches the beginning of the input string) and $ (matches the end of the input string). Among them, square brackets ([]) represent a set of characters, indicating that the username can only be letters, numbers, and underscores. Curly brackets ({}) indicate a range, indicating that the length of the username is between 4 and 16 characters.

Next, we need to add additional rules to verify that the username meets other requirements. Specifically, we need to exclude usernames that start with a number or an underscore, and end with an underscore:

$pattern = '/^[a-zA-Z][a-zA-Z0-9_]{3 ,15}$/';

This regular expression adds a new rule, that is, the username must start with a letter and the length should be between 4 and 16 characters.

Finally, we need to filter out some special characters, such as angle brackets, quotation marks, slashes, etc. This can be achieved by using escape characters () in the character set:

$pattern = '/^[^sa8093152e673feb7aba1828c43532094/\'"]{4,16}$/';

This regular expression uses the ^ (negative) character set to exclude special characters. Note that there cannot be spaces in the character set, so s (space) is used to exclude spaces.

Use preg_match () function to verify the username

Now, with the regular expression, we can use the preg_match() function to check whether the entered username meets the requirements. Here is a sample code:

$ username = "my_name123";
$pattern = '/^[^sa8093152e673feb7aba1828c43532094/\'"]{4,16}$/';
if (preg_match($pattern, $username)) {

echo "用户名合法";

} else {

echo "用户名不合法";

}

In the example, $username is the entered username and $pattern is the regular expression to verify the username. If the username matches the regular expression, "Username is legal" will be output, otherwise "Username is illegal" will be output.

Conclusion

Using PHP regular expressions to verify username format is an important task that can help ensure the data security of registered users and the security of applications. When writing regular expressions, all rules for usernames need to be carefully considered and tested and verified in detail. Use the preg_match() function to conveniently check whether the entered username meets the requirements.

The above is the detailed content of How to verify username format with PHP regular expression. 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