PHP preg_grep -> 搜索词包含特殊字符的搜索数组

PHP preg_grep -> search array where search term contains special character

正在尝试搜索包含特殊字符的数组:

$array=array("0|0Name"=>"first name","0|1last"=>"last name","1|0email"=>"email address");

尝试过

  $v="0|0";
  print_r(preg_grep("/^".$v.".*/",$array)); --->FAIL

尝试过:

  $v=str_replace("|","\|","0|0");
  print_r(preg_grep("/^".$v.".*/",$array)); --->FAIL

使用 preg_quote(),它将转义特殊字符,同时考虑分隔符(在您的情况下,/):

$v = preg_quote("0|0", "/");
print_r(preg_grep("/^".$v.".*/",$array));