2011-03-07 62 views
2

我得到這個錯誤...警告:的preg_match()[function.preg匹配]:未知的修飾

警告:的preg_match()[function.preg匹配]:在未知的修飾詞 '1' C:\ path-to-plugin.php on line 147

當我運行關鍵字「Test $ 2/1 test + word!」時通過以下功能

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

我假設我需要清理關鍵字來轉義「/」字符(可能更多)。我希望你有任何建議,在通過preg_match運行之前必須對字符串進行清理。

UPDATE: 這似乎是由於工作,以泰國:

function my_get_kw_in_content($theKeyword, $theContent) 
    { 
    $theKeyword = preg_quote($theKeyword, '/'); 
    return preg_match('/\b' . $theKeyword . '\b/i', $theContent); 
    } 

回答

6

使用preg_quote引用正則表達式字符。

像這樣:

preg_quote($theKeyword, '/'); 

'/'是在你的正則表達式的分隔符。

+0

泰國人,我用你的答案更新了我的問題。它似乎在爲我工作。你能檢查我的更新以確保我正確地實施你的答案嗎? –

+0

是的,它是正確的。 :) – Thai

相關問題