Add String::findSimilar()

refs #7973
This commit is contained in:
Johannes Meyer 2015-02-12 08:56:10 +01:00
parent c06f015210
commit 33c75a8330
1 changed files with 26 additions and 0 deletions

View File

@ -53,4 +53,30 @@ class String
return $string;
}
/**
* Find and return all similar strings in $possibilites matching $string with the given minimum $similarity
*
* @param string $string
* @param array $possibilities
* @param float $similarity
*
* @return array
*/
public static function findSimilar($string, array $possibilities, $similarity = 0.33)
{
if (empty($string)) {
return array();
}
$matches = array();
foreach ($possibilities as $possibility) {
$distance = levenshtein($string, $possibility);
if ($distance / strlen($string) <= $similarity) {
$matches[] = $possibility;
}
}
return $matches;
}
}