Home > Backend Development > PHP Tutorial > Why Does My PHP Regex Produce 'preg_match(): Compilation failed: invalid range in character class' After Upgrading to PHP 7.3?

Why Does My PHP Regex Produce 'preg_match(): Compilation failed: invalid range in character class' After Upgrading to PHP 7.3?

Susan Sarandon
Release: 2024-12-06 01:37:13
Original
243 people have browsed it

Why Does My PHP Regex Produce

Invalid Range in Character Class: Understanding the Issue After PHP Upgrade

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.

The Shift to PCRE2 in PHP 7.3

With PHP 7.3, the PHP PCRE engine transitioned to PCRE2, causing several backward incompatible changes:

  • The "S" modifier is ineffective, as patterns are automatically studied.
  • The "X" modifier behaves as the default in PCRE2, with no significant impact.
  • Unicode 10 is implemented in PCRE2, potentially introducing behavioral changes with invalid patterns.

Hyphens in Character Classes: Pre- and Post-PHP 7.3

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:

  • Use it at the start or end of the class.

Example:

In the provided code, the problematic line is:

if(!preg_match("/^[a-z0-9]([0-9a-z_-\s])+$/i", $subuser)){
Copy after login

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)){
Copy after login

Additional Reference:

"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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template