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 match a double quote in PHP regex?
- How to use PHP regex to match a zip code?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match a year?
- How to use PHP regex to match an exact string?
- How to use PHP regex with the "x" modifier?
- How to get last matched occurrence in PHP regex?
- How to use PHP regex to match an XML tag?
- How to match a quotation mark in PHP regex?
- How to match a single quote in PHP regex?
See more codes...