2012-05-05 47 views
1

我是新來的正則表達式,我試圖找到使用的preg_match一個字符串,這裏是我的代碼:的preg_match返回未知的修飾詞「C」的錯誤

$artist = $row['ARTIST']; 
$bool = preg_match("/$artist/", $description, $match); 

我的錯誤是:

Unknown modifier 'C' in ... 

如果有人能告訴我我做錯了什麼,我會很感激,謝謝。

回答

3

你要逃避您的變量可能的特殊字符:

$bool = preg_match('/' . preg_quote($artist, '/') . '/', $description, $match); 

preg_quote() in the PHP Manual

preg_quote()以str並提出一個反斜槓每 字符前面是部分正則表達式語法。這是 有用,如果你有一個運行時字符串,你需要匹配一些 文本和字符串可能包含特殊的正則表達式字符。

提示:嘗試呼應你的$artist變量,你應該看到的字符引起的問題

相關問題