preg_match(): Invalid Range in Character Class After PHP Upgrade
The error "preg_match(): Compilation failed: invalid range in character class at offset" typically occurs when a hyphen (-) is used incorrectly within a character class in a regular expression.
In the provided code snippet:
else if(!preg_match("/^[a-z0-9]([0-9a-z_-\s])+$/i", $subuser)){
There is a hyphen "-" within the character class [0-9a-z_-s]. In older versions of PHP, escaping the hyphen with a backslash , or placing it at the beginning or end of the character class, allowed its use.
PHP 7.3 and PCRE2 Changes
However, with PHP 7.3 and the migration to the PCRE2 library, the use of the hyphen is more restricted. In PCRE2, hyphens can only be used at the beginning or end of a character class.
To resolve this issue, modify the character class as follows:
else if(!preg_match("/^[a-z0-9]([0-9a-z_0-9_-])+$/i", $subuser)){
This places the hyphen at the beginning of the character class, allowing it to be used correctly.
Additional Notes
The above is the detailed content of Why Does `preg_match()` Fail with 'Invalid Range in Character Class' After a PHP Upgrade?. For more information, please follow other related articles on the PHP Chinese website!