preg_match_all 问题:编译失败,括号不匹配

Issue with preg_match_all : Compilation failed unmatched parentheses

我正面临以下警告

preg_match_all(): Compilation failed: unmatched parentheses at offset 4

使用时preg_match_all

代码如下

preg_match_all('/' . $word . '/i', $text, $matches);

看起来 $word 上有一些特殊字符用于正则表达式语法。您的 $word 包含以下字符之一:. \ + * ? [ ^ ] $ ( ) { } = ! < > | : - #。您必须使用 preg_quote:

引用这些字符
preg_match_all('/'.preg_quote($word).'/i', $text, $matches);