Jump to content

Strange sequence in error logs: PHP Warning: preg_match_all(): Unknown modifier 'G'


Recommended Posts

I have this function I use to simplify things.

function search_string( $needle, $haystack ) {
    if ( preg_match_all( "/$needle/im", $haystack ) || strpos( $haystack, $needle ) ) {
  	return TRUE;
    }
    return FALSE;
}

I keep getting this error in my PHP logs, and it comes in a sequence:

[07-Nov-2020 05:34:14 America/Los_Angeles] PHP Warning:  preg_match_all(): Unknown modifier 'G' in /home/baser-b/public_html/include/functions.php on line 791

[07-Nov-2020 05:34:14 America/Los_Angeles] PHP Warning:  preg_match_all(): Unknown modifier 'g' in /home/baser-b/public_html/include/functions.php on line 791

Meaning, it will come with one with the small g, then three with the big G, then one with the small g, then five with the big G, and so on....

My question is, how can I stop getting this error. It won't show me the functions being called to arrive at this answer, as this is likely an error generated by another function calling this one. I was wondering if anyone knew what to change in the search_string function to stop getting this error, why this error is happening, or why the strange repetitive sequence. Is it someone trying to do a hack? The only variable that would be changeable by a visitor would be the $needle variable, so what could they type that has something to do with 'g' to get this?

Anyway, thanks.

Link to post
Share on other sites

you will get a false return value if an error occurred (you will get a zero return value for no match.) you can specifically detect the false value and log both the $needle value and the debug_backtrace() information.

Link to post
Share on other sites

hmm, i think that preg_match_all should have three parameters:
preg_match_all ($pattern, $subject, $matches)

the posted code only contains $pattern and $subject (2/3).
preg_match_all( "/$needle/im", $haystack )

php manual entry for preg match all

most helpful text from the php manual:
"Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags."

maybe i am wrong but i think that a proper parameter count will solve the problem.

Link to post
Share on other sites
9 hours ago, baser-b said:

so what could they type that has something to do with 'g' to get this

If $needle contains a slash (/) then it will end your regex early and whatever is after it will be the flags.  Presumable you're calling the function where $needle contains something like "/G".

Do you actually intended for people to enter a regular expression or what?  I don't really see the point of this function.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.