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

丶灬走出姿态 提交于 2019-12-02 05:52:24

问题


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

回答1:


Use preg_quote to quote regular expression characters.

Like this:

preg_quote($theKeyword, '/');

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



来源:https://stackoverflow.com/questions/5215947/warning-preg-match-function-preg-match-unknown-modifier

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