Tag Archives: ErrorException: preg_match(): Compilation failed: invalid range in character class at offset …

[Solved] PHP Error: ErrorException: preg_match(): Compilation failed: invalid range in character class at offset …

One day, the operator came over and said that it was useless to send the verification code by e-mail. As developers, it was our job to solve the problem.

First, check the error log and immediately find that PHP reported an error:

ErrorException: preg_match(): Compilation failed: invalid range in character class at offset 4

The error content means that the regular validation format is wrong and needs to be modified. However, we found no problems in the previous development and use, but recently.

This made us confused until we found that the PHP version of the server was changed from 7.2 to 7.3!

anger

After the difference, we found the cause of the problem:

Upgrade PHP from 7.2 to PHP 7.3

The writing of regular verification format is not standardized

The solution is also simple:

Upgrade the PHP Version (you can not do it, but it is recommended to keep it unified with the server)

Note whether special characters in regular format are escaped. For example, - in [] needs to be escaped: \-