Search code examples
phplevenshtein-distance

Returning multiple matches with levenshtein


How can I return multiple similar matches with levenshtein in PHP? For example if I have this code:

$input = "Hello what is your name. My namer is Jack.";
$output = "nam";
echo levenshtein($input, $output);

It must not only output name, but name, namer.

How can I do that?


Solution

  • Something like:

    $input = "Hello what is your name. My namer is Jack.";
    $output = "nam";
    $threshold = 3;
    foreach(str_word_count($input,1) as $word) {
        if (levenshtein($word, $output) < $threshold) {
            echo $word, PHP_EOL;
        }
    }