I needed the levenshtein function from PHP, but unfortunately are there some restrictions.
I have implemented the levenshtein
function on my own in plain PHP.
You can find the small library at Github.
The library requires PHP 7.
The recommended installtion is via Composer.
composer require gordonlesti/levenshtein
Use GordonLesti\Levenshtein\Levenshtein
.
use GordonLesti\Levenshtein\Levenshtein;
Calculate the levenshtein distance with default costs of 1
for every operation.
$levDist = Levenshtein::levenshtein("AC", "ABAA");
Calculate the levenshtein distance with custom costs like 7
for the insert operation,
9
for the replace operation and 2
for the delete operation for example.
$levDist = Levenshtein::levenshtein("ACCB", "BC", 7, 9, 2);
Here some advantages of gordonlesti/levenshtein
.
There is no limit to the string length, but you should know that the algorithm has a complexity of n*m.
The library supports UTF-8 thanks to the help of Jan Knipper.
$levDist = Levenshtein::levenshtein("♫⚓⚓♥", "♫♥⚓♫⚓♫");
You have the possibility to use float
for the custom costs of the insert, replace or
delete operations.
$levDist = Levenshtein::levenshtein("ACCB", "BC", 7.7, 9.4, 2.5);
The library is written in plain PHP and has a run time minus compared to the implementation in C.