Data to search in:
<?php
$data = array(
0 => 'London Airport',
1 => 'Mancunian fields',
2 => 'Disneyland Florida'
);
Find full string
Searching function:
<?php
/**
* @param array $data
* @param string $what
* @return bool|string
*/
function searchIn($data, $what) {
foreach ($data as $row) {
if (strstr($what, $row)) {
return $row;
}
}
return false;
}
Results:
<?php
// Disney Florida
echo searchIn($data, 'Disneyland Florida in USA');
// Disney Florida
echo searchIn($data, 'Disneyland Florida, USA');
// false
echo searchIn($data, 'whatever Florida Disneyland');
echo searchIn($data, 'No match');
echo searchIn($data, 'London');
Find by any combination of words
Searching function:
<?php
/**
* @param array $data
* @param string $what
* @return int
*/
function searchIn($data, $what) {
$needles = explode(' ', preg_replace('/[^A-Za-z0-9 ]/', '', $what));
foreach ($data as $row) {
$result = false;
foreach ($needles as $needle) {
$stack = explode(' ', $row);
if (!in_array($needle, $stack)) {
continue;
}
$result = $row;
}
if ($result !== false) {
return $result;
}
}
return false;
}
Results:
<?php
// Disneyland Florida
echo searchIn($data, 'Disneyland Florida in USA');
// Disneyland Florida
echo searchIn($data, 'Disneyland Florida, USA');
// Disneyland Florida
echo searchIn($data, 'whatever Florida Disneyland');
// false
echo searchIn($data, 'No match');
// London Airport
echo searchIn($data, 'London');
As you can see, id doesn't matter in what order user is searching and whether or not the string starts with Disneyland.