The error message "preg_match(): Compilation failed: invalid range in character class" indicates a problem with the regular expression used in the provided code. This issue can arise after a PHP upgrade, specifically when migrating from earlier versions to PHP 7.3 or newer due to changes in the PCRE2 library.
With PHP 7.3, the PHP PCRE engine transitioned to PCRE2, causing several backward incompatible changes:
Before PHP 7.3, hyphens could be used within character classes in any position if escaped or placed where they couldn't indicate a range. However, in PHP 7.3 and later, PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL is set to false by default.
Therefore, to include a hyphen in a character class:
In the provided code, the problematic line is:
if(!preg_match("/^[a-z0-9]([0-9a-z_-\s])+$/i", $subuser)){
The issue is with the hyphen (-) within the character class [0-9a-z_-s]. To fix it, place the hyphen at the end or beginning:
if(!preg_match("/^[a-z0-9]([0-9a-z\_-\s0-9a-z\_-\s])+$/i", $subuser)){
"PHP 7.3: PCRE2 has removed PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL" provides further insights:
PCRE2 is more strict in the pattern validations, so after the upgrade, some of your existing patterns could not compile anymore.
Therefore, meticulous scrutiny and modification of existing patterns may be necessary to ensure compatibility with PCRE2 in PHP 7.3 and later versions.
The above is the detailed content of Why Does My PHP Regex Produce 'preg_match(): Compilation failed: invalid range in character class' After Upgrading to PHP 7.3?. For more information, please follow other related articles on the PHP Chinese website!