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);
    }
Scott B
  • 38,833
  • 65
  • 160
  • 266

1 Answers1

6

Use preg_quote to quote regular expression characters.

Like this:

preg_quote($theKeyword, '/');

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

Thai
  • 10,746
  • 2
  • 45
  • 57
  • 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? – Scott B Mar 07 '11 at 05:02