1

I want to search in PHP array , value are fetched from table;

e.g. string is a field in table;

string value are :

1st Value :- Hello my name is harkesh 
2nd Value :- Last name is Chauhan

while($data = mysql_fetch_array($query))
{
$array[] = $data['string']
}

now array is pulling the complete string value but now i want to search for particular words in $array[];

e.g

if i type " Last name" it should show me 2nd Value " Last name is Chauhan" and if i type " name" it should show me both the values as both of them contains "name"

How that can be achieved..Thanks

jeroen
  • 91,079
  • 21
  • 114
  • 132

1 Answers1

2

Could use regex with preg_grep() function to search for the values:

print_r(preg_grep('~\bLast name\b~i', $array));

Array ( 1 => 2nd Value :- Last name is Chauhan )

\b matches a word boundary; Used with i (PCRE_CASELESS) flag

Test at eval.in; SO Regex FAQ for more regex info

Community
  • 1
  • 1
Jonny 5
  • 12,171
  • 2
  • 25
  • 42