Warning: Preg_Replace(): Unknown Modifier 'G'

preg_replace(): Unknown modifier '\\'

preg_replace(): Unknown modifier occurs when $test contains a /.

preg_quote( $test );

should be

preg_quote( $test, '/' );

/ is the PCRE delimiter used in your '/(^|\W)...(\W|$)/i' expression.

PHP PCRE's can have any delimiter, so you have to tell preg_quote() which delimiter is used.

http://php.net/manual/en/regexp.reference.delimiters.php

Warning: preg_replace(): Unknown modifier

Why the error occurs

In PHP, a regular expression needs to be enclosed within a pair of delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character; /, #, ~ are the most commonly used ones. Note that it is also possible to use bracket style delimiters where the opening and closing brackets are the starting and ending delimiter, i.e. <pattern_goes_here>, [pattern_goes_here] etc. are all valid.

The "Unknown modifier X" error usually occurs in the following two cases:

  • When your regular expression is missing delimiters.

  • When you use the delimiter inside the pattern without escaping it.

In this case, the regular expression is <div[^>]*><ul[^>]*>. The regex engine considers everything from < to > as the regex pattern, and everything afterwards as modifiers.

Regex: <div[^>  ]*><ul[^>]*>
│ │ │ │
└──┬──┘ └────┬─────┘
pattern modifiers

] here is an unknown modifier, because it appears after the closing > delimiter. Which is why PHP throws that error.

Depending on the pattern, the unknown modifier complaint might as well have been about *, +, p, / or ) or almost any other letter/symbol. Only imsxeADSUXJu are valid PCRE modifiers.

How to fix it

The fix is easy. Just wrap your regex pattern with any valid delimiters. In this case, you could chose ~ and get the following:

~<div[^>]*><ul[^>]*>~
│ │
│ └─ ending delimiter
└───────────────────── starting delimiter

If you're receiving this error despite having used a delimiter, it might be because the pattern itself contains unescaped occurrences of the said delimiter.

Or escape delimiters

/foo[^/]+bar/i would certainly throw an error. So you can escape it using a \ backslash if it appears anywhere within the regex:

/foo[^\/]+bar/i
│ │ │
└──────┼─────┴─ actual delimiters
└─────── escaped slash(/) character

This is a tedious job if your regex pattern contains so many occurrences of the delimiter character.

The cleaner way, of course, would be to use a different delimiter altogether. Ideally a character that does not appear anywhere inside the regex pattern, say # - #foo[^/]+bar#i.

More reading:

  • PHP regex delimiters
  • http://www.regular-expressions.info/php.html
  • How can I convert ereg expressions to preg in PHP? (missing delimiters)
  • Unknown modifier '/' in …? what is it? (on using preg_quote())

preg_replace: bad regex == 'Unknown Modifier'?

g is not a valid modifier in PCRE (the regex implementation PHP uses) because it's simply not needed; preg_replace() will perform global replacements by default. You'll find the modifier in true Perl regex as well as JavaScript regex, but not in PCRE.

Just drop the g:

$jusr['email'] = preg_replace('/[^a-zA-Z0-9.-_@]/', '', $jusr['email']);

preg_replace_callback(): Unknown modifier '/'

You're correctly attempting to preg-quote your string, but you're not telling it what your delimiter is, so the // inside the string is causing issues. Pass the used delimiter as the second argument, so it can be escaped as well:

$word = '/' . preg_quote($string, '/') . '/i';

preg_match(): Unknown modifier ')'

The main issue is that you failed to properly escape the backslash. You need four backslashes to match a literal backslash in a PHP string literal. Also, if your pattern contains so many backslashes you should think of using a different regex delimiter.

I suggest

 preg_match("~(/.*?/)((?:[^/]|\\\\/)+?)(?:(?<!\\\\)\s|$)~", $line, $matches);

The tilde as a regex delimiter will make the pattern cleaner since there is no longer need to escape backslashes.



Related Topics



Leave a reply



Submit