Skip to content

Instantly share code, notes, and snippets.

@xbill82
Last active August 29, 2015 13:56
Show Gist options
  • Save xbill82/8824481 to your computer and use it in GitHub Desktop.
Save xbill82/8824481 to your computer and use it in GitHub Desktop.
Very fuzzy word-by-word match
/**
* Matches a string over the other in a fuzzy word-by-word fashion.
* The needle matches the haystack if the haystack contains all the words
* of the needle in any order.
*
* @param String $haystack The string to search in.
* @param String $needle The string that has to be searched for in the
* haystack.
* @return Boolean true if the needle matches the haystack, false
* otherwise.
*/
static public function veryFuzzyMatch($haystack, $needle) {
$normalizedNeedle = self::toGenericKey($needle);
$normalizedStack = self::toGenericKey($haystack);
$found = true;
$splitNeedle = explode('-', $normalizedNeedle);
foreach ($splitNeedle as $word) {
if ( stripos($normalizedStack, $word) === false ) {
$found = false;
break;
}
}
return $found;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment