php-regexHow to create a regex for a username in PHP?
A regular expression (regex) can be used to create a username in PHP. The following example code block will create a regex for a username that must be between 6 and 12 characters long and contain only lowercase letters and numbers:
$usernameRegex = '/^[a-z0-9]{6,12}$/';
The output of the example code will be a regex that can be used to validate a username:
/^[a-z0-9]{6,12}$/
Code explanation
/
: This is the start and end delimiter of the regex.^
: This is an anchor that matches the start of the string.[a-z0-9]
: This is a character class that matches any lowercase letter or number.{6,12}
: This is a quantifier that matches between 6 and 12 of the preceding character class.$
: This is an anchor that matches the end of the string.
Helpful links
More of Php Regex
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex with zero or more occurrences?
- How to match a space using PHP regex?
- How to match a single quote in PHP regex?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match special characters?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match a year?
- How to use the "s" modifier in PHP regex?
- How to replace a tag using PHP regex?
See more codes...