Php Preg_Match返回未知修饰符';C';错误

Php Preg_Match返回未知修饰符';C';错误,php,regex,preg-match,Php,Regex,Preg Match,我不熟悉正则表达式,正在尝试使用preg_match查找字符串,以下是我的代码: $artist = $row['ARTIST']; $bool = preg_match("/$artist/", $description, $match); 我的错误是: Unknown modifier 'C' in ... 如果有人能告诉我我做错了什么,我将不胜感激,谢谢。您必须转义变量中可能的特殊字符: $bool = preg_match('/' . preg_quote($artist, '/')

我不熟悉正则表达式,正在尝试使用preg_match查找字符串,以下是我的代码:

$artist = $row['ARTIST'];
$bool = preg_match("/$artist/", $description, $match);
我的错误是:

Unknown modifier 'C' in ...

如果有人能告诉我我做错了什么,我将不胜感激,谢谢。

您必须转义变量中可能的特殊字符:

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

preg_quote()接受str并在每个 作为正则表达式语法一部分的字符。这是 如果您有一个运行时字符串需要在某些方面匹配,则此选项非常有用 文本和字符串可能包含特殊的正则表达式字符

提示:尝试回显您的
$artist
变量,您应该会看到是哪个字符导致了问题