{"record":{"id":"c5321fec3a19a132","repo":"symfony/routing","slug":"cannot-mix-utf-8-requirement-with-non-utf-8-charset-for","errorCode":null,"errorMessage":"Cannot mix UTF-8 requirement with non-UTF-8 charset for variable \"%s\" in pattern \"%s\".","messagePattern":"Cannot mix UTF-8 requirement with non-UTF-8 charset for variable \"(.+?)\" in pattern \"(.+?)\"\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"RouteCompiler.php","lineNumber":194,"sourceCode":"                    preg_quote($defaultSeparator),\n                    $defaultSeparator !== $nextSeparator && '' !== $nextSeparator ? preg_quote($nextSeparator) : ''\n                );\n                if (('' !== $nextSeparator && !preg_match('#^\\{[\\w\\x80-\\xFF]+\\}#', $followingPattern)) || '' === $followingPattern) {\n                    // When we have a separator, which is disallowed for the variable, we can optimize the regex with a possessive\n                    // quantifier. This prevents useless backtracking of PCRE and improves performance by 20% for matching those patterns.\n                    // Given the above example, there is no point in backtracking into {page} (that forbids the dot) when a dot must follow\n                    // after it. This optimization cannot be applied when the next char is no real separator or when the next variable is\n                    // directly adjacent, e.g. '/{x}{y}'.\n                    $regexp .= '+';\n                }\n            } else {\n                if (!preg_match('//u', $regexp)) {\n                    $useUtf8 = false;\n                } elseif (!$needsUtf8 && preg_match('/[\\x80-\\xFF]|(?<!\\\\\\\\)\\\\\\\\(?:\\\\\\\\\\\\\\\\)*+(?-i:X|[pP][\\{CLMNPSZ]|x\\{[A-Fa-f0-9]{3})/', $regexp)) {\n                    throw new \\LogicException(\\sprintf('Cannot use UTF-8 route requirements without setting the \"utf8\" option for variable \"%s\" in pattern \"%s\".', $varName, $pattern));\n                }\n                if (!$useUtf8 && $needsUtf8) {\n                    throw new \\LogicException(\\sprintf('Cannot mix UTF-8 requirement with non-UTF-8 charset for variable \"%s\" in pattern \"%s\".', $varName, $pattern));\n                }\n                $regexp = self::transformCapturingGroupsToNonCapturings($regexp);\n            }\n\n            if ($important) {\n                $token = ['variable', $isSeparator ? $precedingChar : '', $regexp, $varName, false, true];\n            } else {\n                $token = ['variable', $isSeparator ? $precedingChar : '', $regexp, $varName];\n            }\n\n            $tokens[] = $token;\n            $variables[] = $varName;\n        }\n\n        if ($pos < \\strlen($pattern)) {\n            $tokens[] = ['text', substr($pattern, $pos)];\n        }\n","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/symfony/routing/blob/83fa223250b50f4f018c011e101c330e65ac63cc/RouteCompiler.php#L176-L212","documentation":"Opposite of error 77: the route's \"utf8\" option is enabled, but a variable's requirement regex is not valid UTF-8 (preg_match('//u', $regexp) fails), so matching semantics would be inconsistent. compilePattern throws a LogicException naming the variable and pattern.","triggerScenarios":"Compiling a route with options ['utf8' => true] and a requirement containing invalid/non-UTF-8 bytes, e.g. ['slug' => \"caf\\xE9|\"] or a Latin-1 encoded requirement from a config file.","commonSituations":"Requirements loaded from mis-encoded YAML/PHP config files; concatenating encoded user input into requirement strings; enabling utf8 on all routes globally while some legacy requirements are ASCII-escaped bytes.","solutions":["Fix the requirement string so it is valid UTF-8 (mb_check_encoding($regexp, 'UTF-8')).","Re-save the config file with UTF-8 encoding.","Simplify the requirement to an ASCII-safe pattern like [a-z0-9-]+ if unicode matching is unnecessary.","Sanitize programmatically: mb_convert_encoding($requirement, 'UTF-8', 'ISO-8859-1')."],"exampleFix":"// before (requirement bytes are not valid UTF-8)\n$route = new Route('/{slug}', options: ['utf8' => true], requirements: [\"slug\" => \"caf\\xE9\"]);\n\n// after\n$route = new Route('/{slug}', options: ['utf8' => true], requirements: ['slug' => 'café|[a-z-]+']);","handlingStrategy":"validation","validationCode":"if ($route->getOption('utf8')) {\n    foreach ($route->getRequirements() as $name => $req) {\n        if (!mb_check_encoding($req, 'UTF-8')) {\n            throw new \\RuntimeException(\"Requirement for '$name' is not valid UTF-8.\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    (new RouteCompiler())->compile($route);\n} catch (\\LogicException $e) {\n    // fix requirement encoding or drop the utf8 option, then retry\n}","preventionTips":["Keep requirement strings as ASCII regexes whenever possible.","Check config file encodings; ensure YAML is parsed as UTF-8.","Never interpolate raw external bytes into requirement regexes; sanitize first."],"tags":["routing","symfony","utf8","encoding","regex-requirements"],"backgroundTag":"conflicting-config-options","analyzedSha":"83fa223250b50f4f018c011e101c330e65ac63cc","analyzedAt":"2026-09-14T03:19:46.280Z","contentChangedAt":"2026-09-14T03:19:46.280Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}