When does preg_match(): Unknown modifier error occur?
Asked Answered
S

5

3
\\    $DigitalSignature have full name value passed
$SignatureMatch =  '/' . strtolower( $NameFirst . ' ' . $NameLast ) . '$/';
if( true == preg_match( $SignatureMatch, strtolower( $DigitalSignature ) ) )
{
    $boolIsValid = true;
}

I am having this code for exact matching first name and last name match with digital signature. But this gives error reported me in error log on production(live).

preg_match(): Unknown modifier 'b'

I am unable to reproduce this error. How can I get this error firstly. And how to resolve this error for exact matching.

I have seen many questions on SO but not getting when will get this error. And how do I resolve that. Some of questions out of many I have saw are -

  1. Warning: preg_match() [function.preg-match]: Unknown modifier
  2. Unknown modifier in preg_match() statement
  3. Warning: preg_match() [function.preg-match]: Unknown modifier
  4. Unknown modifier 'l' error
  5. Unknown modifier 'g' PHP regex error
  6. Unknown modifier '/' in ...? what is it?
  7. preg_match() Unknown modifier '[' help
  8. Warning: preg_match() [function.preg-match]: Unknown modifier 'v'
  9. PHP Preg_match match exact word
  10. Unknown modifier 'v' when using preg_match() expression in regex
  11. preg_match(); - Unknown modifier '+'
  12. preg_match error Unknown modifier '{'
  13. Unknown modifier '(' when using preg_match() with a REGEX expression
Stanza answered 3/8, 2012 at 10:41 Comment(1)
We'll need to see the resultant pattern rather than the vars that create it.Disincline
E
5

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.

Ermaermanno answered 3/8, 2012 at 10:46 Comment(0)
O
4

$NameFirst or $NameLast may contain a slash /.

You should replace this

$SignatureMatch =  '/' . strtolower( $NameFirst . ' ' . $NameLast ) . '$/';

By this :

$SignatureMatch =  '/' . preg_quote(strtolower( $NameFirst . ' ' . $NameLast ), '/') . '$/';
Outport answered 3/8, 2012 at 10:44 Comment(0)
H
3

One of the strings you're inputting ($NameFirst or $NameLast) contains a /. Use a different delimiter or escape it in the strings.

Also, if you're only checking if a substring is inside a different string, don't use preg_match, use stripos() as it will be much faster.

if (stripos($DigitalSignature ,"$NameFirst $NameLast")) { /* It exists! */ }
Hydrangea answered 3/8, 2012 at 10:44 Comment(4)
I am removing slashes from $DigitalSignature before passing to check.Stanza
@SomnathMuluk: I didn't mean $DigitalSignature.Arrestment
Thanks. I am finally able to reproduce this error. I require exact match. Is it possible with stripos?Stanza
@SomnathMuluk: If you require an exact match, use the equality sign ==. If you need to check that the signature contains the first and last names, use stripos().Arrestment
T
0

You should not be using a regular expression in this case because you are not using any pattern matching. If you just want to find one string inside of another, then use the strpos or strrpos functions: http://php.net/manual/en/function.strpos.php

If it's important that the name be found at the end of the signature, then it's even easier: Take a substring from $signature that is that many characters long from the end.

$fullname = strtolower( "$NameFirst $NameLast" );
$len = strlen($fullname);
$possible_name = substr( $fullname, -$len, $len );
$boolIsValid = ( $possible_name == $fullname );
Tong answered 22/12, 2012 at 5:50 Comment(0)
A
0

If you were using T-Regx, the delimiters would be added for you automatically:

$SignatureMatch =  strtolower($NameFirst . ' ' . $NameLast) . '$';

if (pattern($SignatureMatch, 'i')->matches($DigitalSignature))
{
    $boolIsValid = true;
}
Asset answered 9/10, 2018 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.