When does preg_match(): Unknown modifier error occur?

瘦欲@ 提交于 2019-11-27 15:50:38
deceze

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.

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! */ }

$NameFirst or $NameLast may contain a slash /.

You should replace this

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

By this :

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

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 );

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!