Harkesh Singh Chauhan
Harkesh Singh Chauhan

Reputation: 136

Search words in PHP Array

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

Upvotes: 1

Views: 204

Answers (1)

Jonny 5
Jonny 5

Reputation: 12389

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

Upvotes: 2

Related Questions