Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

preg_match fails with 'Compilation failed: invalid range in character class'

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

Short answer

Inside square brackets a hyphen between two characters means a range. If PHP cannot read your hyphen as a valid range, the pattern fails to compile and preg_match returns false. Escape the hyphen with a backslash or put it first or last in the class. The warning gives the offset where the pattern broke.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the pattern PHP received and count to the offset in the warning. The character at that position is the one that broke the class.

    php -r '$p = "/[\\w-.]+/"; echo $p, PHP_EOL; var_dump(preg_match($p, "a-b"));'
  2. 2Match what you see, then jump to its fix

    1. 1The offset points at the hyphen, for example in [a-z0-9_-.] or [\w-.]A literal hyphen sits between two charactersFix
    2. 2The class has a range that ends at a closing bracket, such as [W-\]46]A closing bracket is the end of a rangeFix
    3. 3The pattern uses /u and a class such as [a-\p{L}]A range ends with a Unicode propertyFix

What does the error look like?

  • Warning: preg_match(): Compilation failed: invalid range in character class at offset 12
  • preg_match() returns false instead of 0 or 1

Cause 1 of 3

A literal hyphen sits between two characters

1Check

The manual says a hyphen between two characters defines a range, such as [d-m]. A hyphen you meant as a plain character is read as a range operator unless it is escaped or placed where it cannot form a range.

grep -rn -E "\[[^]]*[^\\[]-[^]]*\]" src/ | head

2Fix

Escape the hyphen, or move it to the first or last position of the class.

// before:  '/^[a-z0-9_-.]+$/'
'/^[a-z0-9_.\-]+$/'     // escaped
'/^[a-z0-9_.-]+$/'      // last in the class

3Verify

Run the pattern against a sample that contains a hyphen.

php -r 'var_dump(preg_match("/^[a-z0-9_.-]+$/", "my-file.txt"));'

Confirmed by php.net

Did this fix it?

Cause 2 of 3

A closing bracket is the end of a range

1Check

The manual says an unescaped ] cannot be the end character of a range. To end a range at ], escape it or write it in octal or hexadecimal form.

grep -rn "\]-" src/ | head

2Fix

If you want ] as a plain character, escape it and put the hyphen where it cannot form a range. If you want a real range ending at ], use the octal or hexadecimal form.

'/[W\-\]46]/'

3Verify

Run the new pattern on a string that contains a bracket.

php -r 'var_dump(preg_match("/[W\\-\\]46]/", "]"));'

Confirmed by php.net

Did this fix it?

Cause 3 of 3

A range ends with a Unicode property

1Check

The manual says Unicode character properties cannot be part of a range, and that ending a range with one produces a warning.

grep -rn -E "\\\\p\{[A-Za-z]+\}" src/ | head

2Fix

Write the property as its own item in the class, with the hyphen escaped or at the end.

'/[a-z\-\p{L}]+/u'

3Verify

Run the pattern on a sample with letters and a hyphen.

php -r 'var_dump(preg_match("/[a-z\\-\\p{L}]+/u", "ab-é"));'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Put a literal hyphen last in every character class you write, or escape it.
  • Test each new pattern with a sample string and check for false, not only 0.
  • Build patterns from named constants so a changed class is easy to review.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.