0

Trying to search an array that contains special characters:

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

tried

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

tried:

  $v=str_replace("|","\|","0|0");
  print_r(preg_grep("/^".$v.".*/",$array)); --->FAIL
Kevin
  • 41,694
  • 12
  • 53
  • 70
dealerwrx
  • 3
  • 3

1 Answers1

0

Use preg_quote(), it will escape the special characters, taking into account the delimiter (in your case, /):

$v = preg_quote("0|0", "/");
print_r(preg_grep("/^".$v.".*/",$array));
Davey Shafik
  • 360
  • 1
  • 7
  • Thats it! Thanks. Also found that I need to change $array to array_keys($array) to actually find anything :) – dealerwrx Feb 24 '16 at 00:05