0

I'm trying to return all results from a large array that contain 'tags' that were intended to be searched through.

Hopefully I can explain this clearly, the array to be searched through looks like this:

array(
    0 => 'DogCatSheepBlue WhaleShark',
    1 => 'CatPigGoatOtter',
    2 => 'ElephantTigerDogBlue WhaleGoat',
    3 => 'SeahorseSnakeGoatLion',
    ....
);

There's an input box you can enter your search into, so for example if you enter Cat Whale, how can I return results 0 1 and 2 of the array?

When a 'space' is entered into the search box, it should consider those words separately. For example, if you searched GoatSeahorse, it shouldn't return any of the results above, even though Goat and Seahorse are technically both contained in array item 3.

The array is returned from a MySQL query on page load, and the search input is forwarded through jQuery AJAX to the PHP code to create the new, filtered array list, which is then passed back to the HTML page.

Hopefully that makes sense - any advice is greatly appreciated. I'd also be more than happy to hear if there's an easier way to achieve this, even if it involves storing the array or MySQL fields in a different format.

Thanks!

Killdeer
  • 3
  • 1
  • Should it return something if the input is `Pig`? – Nick May 07 '20 at 01:16
  • What have you tried? There are literally thousands of questions here about matching strings in PHP, (eg https://stackoverflow.com/questions/4366730/how-do-i-check-if-a-string-contains-a-specific-word) have you tried using the code they show? If yes, update your question and show it, and explain what didn't work. – Don't Panic May 07 '20 at 01:17
  • Yes, it should return array item ```1```. – Killdeer May 07 '20 at 01:18
  • @Don'tPanic in regards to the thread you linked, using ```strpos()``` would work great if I only had to search for 1 specific word, however in this case the user could search for multiple words but it wouldn't return true if those words were not in the correct order. e.g ``` $a = 'DogSheepGoatPigBlue Whale'; if (strpos($a, 'Dog Sheep') !== false) { echo 'true'; } else { echo 'false'; } ``` It would return ```false``` when the user would expect it to return ```true```. I've been searching through questions for a while before posting. – Killdeer May 07 '20 at 01:31
  • Update your answer, describe that. Pls have a read of [how to ask](https://stackoverflow.com/questions/how-to-ask), and how to create a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). Here's another example: https://stackoverflow.com/questions/15862361/check-if-a-string-contain-multiple-specific-words – Don't Panic May 07 '20 at 01:35
  • @Don'tPanic Nick below has provided an answer to this already - I did search around for a while but I couldn't find a suitable answer but probably due to me not searching the exact right thing though. I also couldn't figure it out looking at the PHP documentation, I'm not an expert by any means. Thanks for the feedback, I will try and word my question better next time! – Killdeer May 07 '20 at 01:46
  • np, I was also trying to encourage you to have a go yourself, using other answers as stepping stones :-) Teach a man to fish, etc. There is a wealth of knowledge here already, it is a very useful skill to be able to use it and adapt an answer that is *almost* what you need. – Don't Panic May 07 '20 at 01:55

1 Answers1

0

One way to do this is to split the input string on space using explode, then creating a regex out of the words (filtered to remove any empty strings caused by multiple spaces in the input), and then filtering the input array using array_filter and preg_match:

function search($array, $search) {
    $words = explode(' ', $search);
    $regex = '/(' . implode('|', array_filter($words)) . ')/';
    return array_filter($array, function ($v) use ($regex) { return preg_match($regex, $v); });
}

You can use the function like this:

$results = array(
    0 => 'DogCatSheepBlue WhaleShark',
    1 => 'CatPigGoatOtter',
    2 => 'ElephantTigerDogBlue WhaleGoat',
    3 => 'SeahorseSnakeGoatLion'
);
print_r(search($results, 'Cat    Whale'));
print_r(search($results, 'GoatSeahorse'));
print_r(search($results, 'Pig'));

Output:

Array
(
    [0] => DogCatSheepBlue WhaleShark
    [1] => CatPigGoatOtter
    [2] => ElephantTigerDogBlue WhaleGoat
)
Array
(
)
Array
(
    [1] => CatPigGoatOtter
)

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95