0

I'm working on a search / advertising system that matches a given ad group with keywords. Query is the string that is the search string, what we're looking for is the best and most efficient way to enhance the simple 'contains' script below that searches through the query, but looks for keyword matches on an AND (&&) explosion. With this script one could build either 'IF' or it could be a "CASE" below is the pseudo code:

$query = "apple berry tomato potato":

if contains ($query,"tomato")  {   }
if contains ($query,"potato,berry")  {   }
if contains ($query,"apple,berry")  {  }

else i.e. none of the above do {  }

the function contains would use strpos but would also use some combination of explode to distinguish words that are separated by commas. So Apple,berry would be where a string contains the list of keywords separated by commas.

What would be the best way to write a contains script that searches through the query string and matches against the comma-separated values in the second parameter? Love your ideas. Thanks.

Here is a the classic simple 'contains' function, but it doesn't handle the comma-separated AND Explosion - it only works with single words or phrases

function contains($haystack,$needle)
{
    return strpos($haystack, $needle) !== false;
}

Note : the enhanced contains function should scan for the match of the string on an AND basis. If commas exist in the $needle it needs to include all of the keywords to show a match. The simple contains script is explained on this post Check if String contains a given word . What I'm looking for is an expanded function by the same name that also searches for multiple keywords, not just a single word.

The $query string will always be space delimited. The $needle string will always be comma delimited, or it could be delimited by spaces.

The main thing is that the function works in multiple directions

Suppose the $query = 'business plan template' or $query = 'templates for business plan'

if you ran contains ($query,"business plan") or contains ($query,"business,plan") both tests would show a match. The sequence of the words should not matter.

Community
  • 1
  • 1
Viktor
  • 381
  • 1
  • 20
  • I've updated the post and included a simple contains script that is a starting point for the more deluxe explosion script that searches for multiple keywords. – Viktor Apr 20 '17 at 17:57
  • So you're looking to match ALL comma separated words in $query and $query is always space delimited? – AbraCadaver Apr 20 '17 at 17:58
  • Yes - $query is space delimited and the $needle is comma delimited – Viktor Apr 20 '17 at 18:03

1 Answers1

1

Here's a simple way. Just compare the count of $needle with the count of $needle(s) that are in $haystack using array_intersect():

function contains($haystack, $needle) {
    $h = explode(' ', $haystack);
    $n = explode(',', $needle);
    return count(array_intersect($h, $n)) == count($n);
}

You could optionally pass $needle in as an array and then no need for that explode().

If you need it case-insensitive:

$h = explode(' ', strtolower($haystack));
$n = explode(',', strtolower($needle));
AbraCadaver
  • 73,820
  • 7
  • 55
  • 81