preg_match (): compilation error: invalid range in character class with offset

Thank you in advance for taking the time to help in this matter.

preg_match (): compilation error: invalid range in character class at offset 20 session.php on line 278

This stopped working suddenly after several months of work, after updating PHP on our server.

Here is the code

else{ /* Spruce up username, check length */ $subuser = stripslashes($subuser); if(strlen($subuser) < $config['min_user_chars']){ $form->setError($field, "* Username below ".$config['min_user_chars']."characters"); } else if(strlen($subuser) > $config['max_user_chars']){ $form->setError($field, "* Username above ".$config['max_user_chars']."characters"); } /* Check if username is not alphanumeric */ /* PREG_MATCH CODE */ else if(!preg_match("/^[a-z0-9]([0-9a-z_-\s])+$/i", $subuser)){ $form->setError($field, "* Username not alphanumeric"); } /* PREG_MATCH CODE */ /* Check if username is reserved */ else if(strcasecmp($subuser, GUEST_NAME) == 0){ $form->setError($field, "* Username reserved word"); } /* Check if username is already in use */ else if($database->usernameTaken($subuser)){ $form->setError($field, "* Username already in use"); } /* Check if username is banned */ else if($database->usernameBanned($subuser)){ $form->setError($field, "* Username banned"); } } 
+25
php regex preg-match
source share
5 answers

The range of character classes is determined by - between two values ​​in a character class ( [] in a regular expression). [0-9] means everything between 0 and 9 inclusive. In the regular expression in your code, you have several ranges of character classes, az , 0-9 . There is also one class that you probably did not want to specify, namely _-\s .

 "/^[a-z0-9]([0-9a-z_-\s])+$/i" ^^^^ 

This, apparently, is not considered an invalid character range in some (most?) Versions of PCRE (using the PHP regular expression library), but recently it can be changed, and if the PCRE library was updated on the server, it could be cause.

Debuggex is a good tool that can help debug errors (well, the error message from PHP told you both the line and the symbol where the error was, so ..) like this (I'm not affiliated, just a fan).

+23
source share

Your mistake depends on your regular expression translator.

You can avoid the hyphen in order to understand how to use it . That is, using \- instead of - .

Your final code:

 /^[a-z0-9]([0-9a-z_\-\s])+$/i 
+16
source share

The problem is really old, but there are some new developments related to PHP 7.3 and newer versions that need to be covered. The PHP PCRE engine migrates to PCRE2 , and the PCRElibrary version used in PHP 7.3 is 10.32, and that is where the backward incompatible changes come from:

  • The internal library API has changed
  • The 'S' modifier has no effect; patterns are learned automatically. No real impact.
  • The β€œX” modifier is the default behavior in PCRE2. The current patch returns the behavior to the value β€œX”, as it was in PCRE, but it might be better to go with the new behavior and enable β€œX” by default. So at the present time there is no influence either.
  • Some behavior change was noticed due to the newer Unicode engine. This is Unicode 10 in PCRE2 versus Unicode 7 in PCRE. Some change in behavior can be seen with invalid patterns.

Accuracy. to PHP 10.33 change log:

  1. With PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL escape sequences set, such as \s that are valid in character classes, but not as the end of ranges, were treated as literals. An example is [_-\s] (but not [\s-_] , because it gave an error at the beginning of the range). Now the error "Invalid range" is issued regardless of PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL .

Prior to PHP 7.3, you could use a hyphen in a character class at any position if you avoided it, or if you placed it "in a position where it cannot be interpreted as indicating a range . " In PHP 7.3, it seems that PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL been set to false. So now, to put a hyphen in a character class, always use it only at the start or end position .

See also this link :

Simply put,

PCRE2 is more strict in checking templates, so after updating some of the existing templates can no longer compile.

Here is a simple snippet used in php.net

  preg_match ('/ [\ w -.] + /', '');  // this will not work in PHP7.3
 preg_match ('/ [\ w \ -.] + /', '');  // the hyphen need to be escaped 

As you can see from the above example, there is a small but significant difference between the two lines.

+9
source share

I have this error and I solve it by doing this

Route::get('{path}','HomeController@index')->where( 'path', '([Az]+)?' );

and this is work for me.

-one
source share

Firstly, Route :: get ('{path}', "HomeController @index") β†’ where ('path', '([Az \ d - / _.] +)?');

this does not work for me when I try from below, and this happens :-)

Route :: get ('{path}', 'HomeController @index') β†’ where ('path', '([Az] +)?')

-2
source share

All Articles