开发者

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

This question already has answers here: Warning: preg_replace(): Unknown modifier (3 answers) Closed 3 years ago.

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


Use preg_quote to quote regular expression characters.

Like this:

preg_quote($theKeyword, '/');

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜