Unknown modifier
PHP stopped the pattern at its second delimiter, and what follows it is not a list of modifiers.
preg_match('/^[a-z]+/[0-9]+$/', $value);
// preg_match(): Unknown modifier '['
The pattern holds three slashes. The first opens it, and the second, after [a-z]+, closes it: the pattern is ^[a-z]+, and PHP reads [0-9]+$/ as the modifiers. [ is not one, so the call returns false and nothing is compiled. preg_match_all(), preg_replace(), preg_split() and the others print the same warning under their own name; the replacing functions return null.
Why PHP prints it
A delimiter closes the pattern only when nothing escapes it. PHP finds it by looking for the next delimiter without a backslash, and that look knows nothing of a character class, a comment or a quote. Two situations follow.
A delimiter stands inside the pattern. The pattern ends there, and the rest is read as modifiers.
preg_match('/https://example\.com/i', $url);
// preg_match(): Unknown modifier '/'
A modifier PHP does not know. The modifiers are i, m, s, x, u, A, D, S, U, X, J and n, with r from PHP 8.4 (the list on php.net). A letter of another language, a capital I, or the e of PHP 5:
preg_match('/foo/g', $text);
// preg_match(): Unknown modifier 'g'
preg_match('/foo/e', $text);
// preg_match(): Unknown modifier 'e'
e ran the replacement of preg_replace() as PHP code. PHP 7.0 removed it, and since 8.4 its letter is no longer known: the warning above is the one PHP 8.4 and later print. PHP 7.0 to 8.3 name the removal instead: The /e modifier is no longer supported, use preg_replace_callback instead. Either way preg_replace_callback() does the same work.
There is no g modifier in PHP
The g of JavaScript and Perl says: every match. PHP has no such letter, and needs none: preg_replace() and preg_split() act on every match by default, preg_match_all() finds all of them, and preg_match() stops at the first.
preg_replace('/foo/g', 'x', 'foo foo');
// preg_replace(): Unknown modifier 'g'
// preg_replace() replaces every occurrence, and the third argument limits them
var_dump(preg_replace('/foo/', 'x', 'foo foo')); // string(3) "x x"
var_dump(preg_replace('/foo/', 'x', 'foo foo', 1)); // string(5) "x foo"
// preg_match_all() finds every occurrence, where preg_match() stops at the first
var_dump(preg_match_all('/foo/', 'foo foo', $matches)); // int(2)
The modifier page of the guide says the rest: PHP regex modifiers.
How to fix it
Escape the delimiter where it stands, or choose a delimiter the pattern does not hold. The second reads better when the delimiter appears more than once, as in a date, a path or a URL.
// The delimiter escaped
preg_match('/^[a-z]+\/[0-9]+$/', $value);
// Another delimiter: nothing to escape
preg_match('#^[a-z]+/[0-9]+$#', $value);
// A URL keeps its slashes
preg_match('~https://example\.com~i', $url);
When a variable builds the pattern, preg_quote() escapes the delimiter with it: preg_quote($path, '#') escapes # as well as the metacharacters. A modifier that is not known is a misspelling, or a flag of another language: the g of JavaScript and Perl has no counterpart here, since preg_match_all() finds every match.
Where it hides
- A character class.
/[/]/protects nothing: the delimiter ends the pattern inside the class, and PHP reads]as the modifier. - A comment. In
/a(?#comment with / inside)b/the delimiter ends the pattern in the middle of the comment. - A
\Q…\Equote. In/\Qa/b\E/the quoted text is literal to PCRE2, not to PHP. - An HTML entity. A pattern pasted from a page can hold
where a newline was meant: PHP reads&as a modifier. - A doubled delimiter.
/foo//icloses the pattern at the second slash, and reads the third as a modifier.
What PHPRegex reports
PHPRegex reads the pattern as PHP does, and names the cause:
regex.delimiter.unescapedfor/^[a-z]+/[0-9]+$/: “Unescaped delimiter “/” at position 7 ends the pattern early; what follows is read as modifiers. Escape it as “\/” or use another delimiter.”regex.flag.unknownfor/foo/g: “Unknown regex flag(s) found: “g”.”regex.flag.removedEfor/foo/e: theeflag was removed in PHP 7.0, andpreg_replace_callback()replaces it.
Open /^[a-z]+/[0-9]+$/ in the playground: the Diagnostics tab shows the code, its message, and what PHP says.
Related
- No ending delimiter found, the other message a misplaced delimiter gives.
- Delimiter must not be alphanumeric, backslash, or NUL byte.
- Regex in PHP: delimiters and modifiers from the start.
- Useless Flag ‘i’ (Caseless): the linter also reports a modifier that changes nothing.