Accept-Language Header Value Regex for PHP
/^[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?(?:\s*,\s*[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?)*$/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching accept-language header value, ported and verified for PHP. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.
Php Implementation
<?php
// Accept-Language Header Value
// ReDoS-safe | RegexVault — Web & Network > HTTP
define('ACCEPTLANGUAGE_HEADER_VALUE_PATTERN', '/^[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?(?:\s*,\s*[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?)*$/');
function validate_acceptlanguage_header_value(string $input): bool {
return (bool) preg_match(ACCEPTLANGUAGE_HEADER_VALUE_PATTERN, $input);
}
// Example
var_dump(validate_acceptlanguage_header_value("en")); // bool(true)Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
en | en;q=1.5 |
en-US | en;q=-0.1 |
en-US,en;q=0.9,fr;q=0.8 | 123 |
zh-CN,zh;q=0.9,en;q=0.8 | en-US, fr;q=abc |
de-CH-x-phonebk | en-US;;q=0.9 |
When to use this pattern
This pattern is drawn from the Web & Network > HTTP category and carries a ReDoS-safe certification. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
An empty Accept-Language value is not valid — some clients send * (any language) as a wildcard, which this pattern does not match. Add \* as an additional alternative if needed.
Technical Notes
Quality factor (q) must be between 0 and 1 with up to 3 decimal places. Default q value is 1.0 when omitted. Used for content negotiation.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern