在preg_match php中使用变量中的模式返回false
我想检查字符串是否包含可变数量的生成单词。这是我编写的代码,它将模式存储在我放入preg_匹配中的变量中 此代码的结果:返回true在preg_match php中使用变量中的模式返回false,php,string,preg-match,Php,String,Preg Match,我想检查字符串是否包含可变数量的生成单词。这是我编写的代码,它将模式存储在我放入preg_匹配中的变量中 此代码的结果:返回true $string = "What a beatiful world"; $pieces = "#\b(" . implode("|",$pieces_arr). ")\b#"; $pieces输出,例如: #\b(hello|you)\b# 进入: 删除单引号。在这种情况下,preg_quote()通常是可取的。删除单引号后,每个结果都是正确的。我来看看pre
$string = "What a beatiful world";
$pieces = "#\b(" . implode("|",$pieces_arr). ")\b#";
$pieces
输出,例如:
#\b(hello|you)\b#
进入:
删除单引号。在这种情况下,
preg_quote()
通常是可取的。删除单引号后,每个结果都是正确的。我来看看preg\u quote()
。更新了帖子。
if(preg_match($pieces, $string)) {
echo "piece found in string!!";
}