Validating Strings as Alphanumeric Symbols Using Regex in PHP
To check if a string contains only alphanumeric symbols using regular expressions, use ^[ -~]+$
. This regular expression matches strings composed of one or more alphanumeric symbols, such as "Hello, world!". If you want to exclude spaces, modify the pattern to ^[!-~]+$
.
Additionally, if you want to match a fixed-length alphanumeric string, use the pattern ^[ -~]{n}$
. For example, ^[ -~]{6}$
matches a string exactly 6 characters long. You can also specify minimum and maximum lengths to create flexible patterns. For example, to match strings between 12 and 20 characters long, use ^[ -~]{12,20}$
.
Below are some commonly used regular expression patterns for validating alphanumeric symbols. Feel free to utilize them as needed.
# | Match Condition | Regular Expression Pattern |
---|---|---|
1 | Contains only alphanumeric symbols | ^[ -~]+$ |
2 | Fixed length of n alphanumeric symbols | ^[ -~]{n}$ |
3 | At least n alphanumeric symbols | ^[ -~]{n,}$ |
4 | No more than m alphanumeric symbols | ^[ -~]{1,m}$ |
5 | Between n and m alphanumeric symbols | ^[ -~]{n,m}$ |
Source Code
Here is a PHP function to validate if an input string consists only of alphanumeric symbols. The function allows validation based on the following conditions:
- If the minimum length is omitted: The string must consist only of alphanumeric symbols and not exceed the specified maximum length.
- If the maximum length is omitted: The string must consist only of alphanumeric symbols and meet the minimum length requirement.
- If both minimum and maximum lengths are omitted: The string must consist entirely of alphanumeric symbols.
/**
* Checks whether a given string consists only of alphanumeric characters and symbols.
*
* This function verifies that the string consists only of alphanumeric characters
* and symbols within the ASCII range (0x20 to 0x7E) and that its length falls
* within the specified range.
*
* @param string $str The input string.
* @param ?int $minLength The minimum number of characters (treated as 1 if null).
* @param ?int $maxLength The maximum number of characters (treated as the length of the string if null).
* @return bool Returns true if the input string meets the conditions; otherwise, returns false.
* @throws InvalidArgumentException Throws an exception if the minimum length is less than 1,
* or if the maximum length is less than the minimum length.
*/
function isAlphanumericSymbols(string $str, ?int $minLength = null, ?int $maxLength = null): bool {
// Set default values
$min = $minLength ?? 1;
$max = $maxLength ?? mb_strlen($str);
// Validate arguments
if ($min < 1) {
// Throw an exception if the minimum length is less than 1
throw new InvalidArgumentException('The minimum length must be an integer greater than or equal to 1.');
}
if (!is_null($maxLength) && $max < $min) {
// Throw an exception if the maximum length is less than the minimum length
throw new InvalidArgumentException('The minimum length must not exceed the maximum length.');
}
// Construct the regular expression pattern for alphanumeric characters and symbols
$pattern = is_null($maxLength)
? sprintf('/^[ -~]{%d,}$/', (int)$min) // No limit on the maximum number of characters
: sprintf('/^[ -~]{%d,%d}$/', (int)$min, (int)$max);
// Perform the validation using regular expressions
return (bool)preg_match($pattern, $str);
}
Validation
Please specify the range in characters, not bytes.