1. Home
  2. Php大法

php正则函数错误 preg_match(): Compilation failed: invalid range in character class at offset

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

这个错误的发生位置在wordpress编辑器代码高亮工具 Crayon Syntax Highlighter 安装后,当我们添加新文章进入编辑器的时候出现的


  function __construct($id, $name = NULL) {
        parent::__construct($id, $name);
        $this->modes = CrayonParser::modes();

    // Override
    function clean_id($id) {
        $id = CrayonUtil::space_to_hyphen( strtolower(trim($id)) );
        return preg_replace('/[^\w-+#]/msi', '', $id);


return preg_replace('/[^\w-+#]/msi', '', $id);


return preg_replace('/[^\w\-+#]/msi', '', $id);
下面引用自stackflow上面的描述大概意思是php的正则library pcre从php7.3开始更新未pcre2,然后语法会越来越严格,大概就是这个意思

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

  • Internal library API has changed
  • The 'S' modifier has no effect, patterns are studied automatically. No real impact.
  • The 'X' modifier is the default behavior in PCRE2. The current patch reverts the behavior to the meaning of 'X' how it was in PCRE, but it might be better to go with the new behavior and have 'X' turned on by default. So currently no impact, too.
  • Some behavior change due to the newer Unicode engine was sighted. It's Unicode 10 in PCRE2 vs Unicode 7 in PCRE. Some behavior change can be sighted with invalid patterns.

Acc. to the PHP 10.33 changelog:

  1. With PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL set, escape sequences such as \s which are valid in character classes, but not as the end of ranges, were being treated as literals. An example is [_-\s] (but not [\s-_] because that gave an error at the startof a range). Now an "invalid range" error is given independently of PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL.

Before PHP 7.3, you might use the hyphen in a character class in any position if you escaped it, or if you put it "in a position where it cannot be interpreted as indicating a range". In PHP 7.3, it seems the PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL was set to false. So, from now on, in order to put hyphen into a character class, always use it either at the start or end positions only.

See also this reference:

In simple words,

PCRE2 is more strict in the pattern validations, so after the upgrade, some of your existing patterns could not compile anymore.

Here is the 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 example above there is a little but substantial difference between the two lines.


Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us




QR code