✅ UTF-8 supported Levenshtein PHP function. A Function that finds similarity distance between two strings.
MIT License
Levenshtein PHP function with UTF-8 support. This function finds similarity distance between two strings.
Returns in Levenshtein Distance. (The smaller, the closer)
mb_levenshtein ( string $str1 , string $str2 ) : int
mb_levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del ) : int
Returns in Levenshtein Ratio between 0 to 1. (The bigger, the closer)
mb_levenshtein_ratio ( string $str1 , string $str2 ) : float
mb_levenshtein_ratio ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del ) : float
<?php
include_once('./mb_levenshtein.php');
$query = 'cafe';
$comps = [
'coffee',
'caf',
'tea',
'sake',
];
echo "Query word: ${query}" . PHP_EOL;
foreach ($comps as $comp) {
$sim = mb_levenshtein($query, $comp);
echo " ${comp}: ${sim}" . PHP_EOL;
}
Results:
$ # The smaller, the closer
$ php ./sample.php
Query word: cafe
coffee: 3
caf: 1
tea: 4
sake: 2
To use released version:
composer require keinos/mb_levenshtein
To use latest version:
composer require keinos/mb_levenshtein:dev-master
<?php
require_once('vendor/autoload.php');
$query = 'cafe';
$comps = [
'coffee',
'caf',
'tea',
'sake',
];
echo "Query word: ${query}" . PHP_EOL;
foreach ($comps as $comp) {
$sim = mb_levenshtein($query, $comp);
echo " ${comp}: ${sim}" . PHP_EOL;
}
Results:
$ ls
sample.php
$ composer require keinos/mb_levenshtein
Using version ^1.0 for keinos/mb_levenshtein
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing keinos/mb_levenshtein (1.0.0): Downloading (100%)
Writing lock file
Generating autoload files
$ # The smaller, the closer
$ php ./sample.php
Query word: cafe
coffee: 3
caf: 1
tea: 4
sake: 2
$ ls
composer.json composer.lock sample.php vendor
$ cat composer.json
{
"require": {
"keinos/mb_levenshtein": "^1.0"
}
}