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

PHP regular expression in action: matching usernames

王林
王林Original
2023-06-23 08:42:121414browse

In web development, it is often necessary to perform format restrictions or format checks on data. PHP regular expressions are one of the powerful tools for such operations. In this article, we will explain through actual combat and teach you how to use PHP regular expressions to match user names.

1. Username requirements

First of all, we need to clarify the username requirements so that we know what kind of regular expression needs to be used for matching. Normally, the username requirements are as follows:

  • consists of letters, numbers or underscores;
  • is between 6 and 20 characters in length;
  • does not end with Starts with a number;
  • does not allow special characters.

Based on these requirements, we can start writing regular expressions.

2. Write regular expressions

Let’s first analyze the regular expressions that need to be used for each requirement:

  • consists of letters, numbers or underscores .

This requirement is easy to meet, just use the character set [0-9a-zA-Z_].

  • Length is between 6 and 20 characters.

This requirement requires the use of quantifiers, that is, {6,20} means matching 6 to 20 characters. We need to add the character set to the character set, and finally get: [0-9a-zA-Z_]{6,20}.

  • does not start with a number.

This requirement requires the use of a zero-width assertion, that is, adding (?=D) before the character set indicates that the match must start with a non-digit. Finally we get: (?=D)[0-9a-zA-Z_]{6,20}.

  • Special characters are not allowed.

This requirement requires the exclusion of special characters. We can use the exclusion character set 1. Since there are many character sets that need to be excluded, we can use the Unicode encoding range to represent the character sets that need to be excluded: [u4e00-u9fa5uFE30-uFFA0u3000-u303F]|[s~!@#$%^&*()_ ={}[]|\;:'",.a8093152e673feb7aba1828c43532094/?·!¥......()—""[],;':",. ,? ]. Finally get: (?=D)[0-9a-zA-Z_]{6,20}(?![u4e00-u9fa5uFE30-uFFA0u3000-u303F]|[s~!@#$%^&*( )_ ={}[]|\;:'",.a8093152e673feb7aba1828c43532094/?·!¥......()—""[],;':",.,?]).

3. Use regular expressions for matching

Now that we have written the regular expression, we can use PHP's preg_match function to match. The following is a sample code:

function matchUsername($username) {
    $pattern = '/^(?=D)[0-9a-zA-Z_]{6,20}(?![u4e00-u9fa5uFE30-uFFA0u3000-u303F]|[s`~!@#$%^&*()_+={}[]|\;:'",.<>/?·!¥……()—《》【】、;‘:“,。、?])$/';
    if(preg_match($pattern, $username)) {
        return true;
    }
    return false;
}

$username1 = 'abcde';
$username2 = 'abcd@e';
$username3 = '1abcde';
$username4 = '123456789abcdefghi';
$username5 = '张三';
$username6 = 'abcdef张三';

var_dump(matchUsername($username1)); // true
var_dump(matchUsername($username2)); // false
var_dump(matchUsername($username3)); // false
var_dump(matchUsername($username4)); // false
var_dump(matchUsername($username5)); // false
var_dump(matchUsername($username6)); // false

In the code, we define a matchUsername function to match, and use test cases to verify each. After running the code, you can see that except $username1, the rest of the test cases failed to match.

So far, we have explained how to use PHP regular expressions to match user names through actual combat. We hope it will be helpful to beginners.


  1. Special character set

The above is the detailed content of PHP regular expression in action: matching usernames. 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