Warning: preg_match() [function.preg-match]: Unknown modifier [duplicate]
Asked Answered
M

1

2

I'm getting this error...

Warning: preg_match() [function.preg-match]: Unknown modifier '1' in C:\path-to-plugin.php on line 147

When I run the keyword "Test $2/1 test+word!" through the function below

function my_get_kw_in_content($theKeyword, $theContent)
    {
//ERROR OCCURS NEXT LINE
    return preg_match('/\b' . $theKeyword . '\b/i', $theContent);
    }

I'm assuming that I need to sanitize the keyword to escape the "/" character (and possibly more). I'd appreciate any suggestions you have to sanitize the string before running it through the preg_match.

UPDATE: This appears to work thanks to Thai:

function my_get_kw_in_content($theKeyword, $theContent)
    {
    $theKeyword = preg_quote($theKeyword, '/');
    return preg_match('/\b' . $theKeyword . '\b/i', $theContent);
    }
Marqueritemarques answered 7/3, 2011 at 4:50 Comment(0)
T
6

Use preg_quote to quote regular expression characters.

Like this:

preg_quote($theKeyword, '/');

Where '/' is the delimiter in your regular expression.

Tompion answered 7/3, 2011 at 4:53 Comment(1)
Thai, I've updated my question with your answer. It appears to be working for me. Can you check my update to insure I'm implementing your answer properly?Marqueritemarques

© 2022 - 2024 — McMap. All rights reserved.