Preg_Match(); - Unknown Modifier '+'

preg_match(); - Unknown modifier '+'

You need to use delimiters with regexes in PHP. You can use the often used /, but PHP lets you use any matching characters, so @ and # are popular.

Further Reading.

If you are interpolating variables inside your regex, be sure to pass the delimiter you chose as the second argument to preg_quote().

preg_match() Unknown modifier '[' help

Try the following:

<?php
$str = "http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I";
$pattern = '#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#';
preg_match($pattern, $str, $matches);
print_r($matches);
?>

Note, I'm using # as a delimiter here simply because the regular expression above contains forward slashes and escaping them makes the expression more difficult to read. This cleans it up by just a few pixels.

what causes Warning: preg_match(): Unknown modifier 'p'

Everything after second / (closing delimiter) is considered flags. Use another delimiter such as ~

~^image/p?jpeg$~i

Or to match the delimiter literal inside the pattern escape it using the backslash:

/^image\/p?jpeg$/i

Most comfortable to pick a delimiter, that you don't need inside the pattern > no worry for escaping. Frequently used delimiters are /,~,#,@ or even bracket style delimiters such as ( pattern ).


side note


You could combine all three preg_match to a single one using alternation:

if(preg_match('~^image/(?:gif|p?jpeg|(?:x-)?png)$~i', $_FILES['upload']['type'])) { ... }

(?: starts a non-capture group. Good for testing: regex101.com

preg_match(): Unknown modifier 'h' in php

$item has a / in it, which means that it thinks that the regex ends sooner than it does.

/goldfish/herring/
// ^ Not actually a modifier (h) - just an unescaped string

You can use the preg_quote($string, '/') function for this to turn it into the following:

/goldfish\/herring/
// ^ Escaped

Usage:

 foreach($items as $k=>$item){
if($item!='' && preg_match("/".preg_quote($item,"/")."/", $opText)){
if(!in_array($item,$params[$val['name']],true)){
$params[$val['name']][]=$item;
}
}
}

Tip:

If this is a hardcoded regex, you can change your escape character to make the regex easier to read by not having to escape a commonly used character:

~goldfish/herring~
// ^ ^
// | \- Using a different modifier
// |
// \- Different from the modifier, so we don't have to escape it

When does preg_match(): Unknown modifier error occur?

If the first name or last name contains a /, your regex will look something like:

/john/doe$/

To preg_match, this looks like the regex is /john/, with the trailing doe$/ being the modifiers. Those are of course invalid modifiers. You need to escape the regex delimiters (/) inside the regex itself using preg_quote.

PHP | preg_match(): Unknown modifier '(' in C:\xampp\htdocs\Folder\index.php on line 38

You forgot one "\" before "/)" on your pattern :

$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";

So php is thinking that "/(" is a modifier : http://php.net/manual/sr/reference.pcre.pattern.modifiers.php

Warning: preg_match(): Unknown modifier '.' after replacing ereg

  1. "." needs a "\" to avoid it be taken as any character.
  2. the pattern needs delimiters for preg_match.

    $pattern = "|([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)|";

Warning: preg_match(): Unknown modifier ' '

preg_match arguments are in the wrong order. First should be the regular expression.

So change:

preg_match($source_html, $rule, $matches);

to:

preg_match($rule, $source_html, $matches);

getting error: Warning: preg_match() [function.preg-match]: Unknown modifier ',' in

You forgot to escape your slashes which are your regex delimiters:

if (preg_match('/[0-9.-]{1,}/,/[0-9.-]{1,}/', $location, $regs))

should be

if (preg_match('/[0-9.-]{1,}\/,\/[0-9.-]{1,}/', $location, $regs))


Related Topics



Leave a reply



Submit