preg_match fails with 'Compilation failed: invalid range in character class'
Warning: preg_match(): Compilation failed: invalid range in character class at offset 12Short 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.
Fix it fast
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"));'2Match what you see, then jump to its fix
- 1
The offset points at the hyphen, for example in [a-z0-9_-.] or [\w-.]A literal hyphen sits between two charactersFix - 2
The class has a range that ends at a closing bracket, such as [W-\]46]A closing bracket is the end of a rangeFix - 3
The pattern uses /u and a class such as [a-\p{L}]A range ends with a Unicode propertyFix
- 1
What does the error look like?
Warning: preg_match(): Compilation failed: invalid range in character class at offset 12preg_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/ | head2Fix
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 class3Verify
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
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/ | head2Fix
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
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/ | head2Fix
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
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix