php preg_grep精确匹配

php preg_grep精确匹配,php,grep,Php,Grep,好吧现在我用 Preg_grep("/$query/", $file); 这基本上是通过postmethod发送的文本&检查它是否在$file中,但我可以简单地键入“a”,它将输出每个匹配项,我希望它更具体 例如,如果我输入$file中的“Customer123”,它将/应该输出“Customer123”,但是如果我输入“Customer”,那么这不足以输出Customer123,因此返回“notfound” 您可以在表达式前后使用\b将其作为一个完整的单词进行匹配 preg_grep("/\

好吧现在我用

Preg_grep("/$query/", $file);
这基本上是通过postmethod发送的文本&检查它是否在$file中,但我可以简单地键入“a”,它将输出每个匹配项,我希望它更具体


例如,如果我输入$file中的“Customer123”,它将/应该输出“Customer123”,但是如果我输入“Customer”,那么这不足以输出Customer123,因此返回“notfound”

您可以在表达式前后使用
\b
将其作为一个完整的单词进行匹配

preg_grep("/\b$query\b/", $file);
输出:-

检查是否为空:-

输出:-

if(trim($query) == ''){
   var_dump(preg_grep("/(^\$query*$)/", $file));

}else{
  var_dump(preg_grep("/\b$query\b/", $file));
}