Skip to content

Instantly share code, notes, and snippets.

@abcarroll
Last active August 29, 2015 14:19
Show Gist options
  • Save abcarroll/9c7cb4dab4a20791b071 to your computer and use it in GitHub Desktop.
Save abcarroll/9c7cb4dab4a20791b071 to your computer and use it in GitHub Desktop.
str_distance_array()
<?php
/* A.B. Carroll, public domain */
function str_distance_array($needle, $haystack) {
$confidence_map = [];
foreach ($haystack as $h) {
if(strlen($needle) < strlen($h)) {
$normalize = strlen($needle);
} else {
$normalize = strlen($h);
}
$confidence_map[$h] = $normalize - (levenshtein($needle, $h) * 2);
$confidence_map[$h] += similar_text($needle, $h);
}
natsort($confidence_map);
$confidence_map = array_reverse($confidence_map);
return $confidence_map;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment