https://github.com/jfcherng/php-levenshtein-distance
Calculates the Levenshtein distance and the edit progresses between two strings.
https://github.com/jfcherng/php-levenshtein-distance
edit-distance edit-progress levenshtein-distance php-71 psr-1 psr-2 psr-4
Last synced: 18 days ago
JSON representation
Calculates the Levenshtein distance and the edit progresses between two strings.
- Host: GitHub
- URL: https://github.com/jfcherng/php-levenshtein-distance
- Owner: jfcherng
- License: mit
- Created: 2018-06-24T12:59:17.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-11-06T20:52:36.000Z (6 months ago)
- Last Synced: 2025-04-09T20:03:50.975Z (18 days ago)
- Topics: edit-distance, edit-progress, levenshtein-distance, php-71, psr-1, psr-2, psr-4
- Language: PHP
- Size: 203 KB
- Stars: 2
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# php-levenshtein-distance
[](https://github.com/jfcherng/php-levenshtein-distance/actions)
[](https://packagist.org/packages/jfcherng/php-levenshtein-distance)
[](https://packagist.org/packages/jfcherng/php-levenshtein-distance)
[](https://github.com/jfcherng/php-levenshtein-distance/blob/master/LICENSE)
[](https://github.com/jfcherng/php-levenshtein-distance/stargazers)
[](https://www.paypal.me/jfcherng/5usd)Calculate the Levenshtein distance and edit progresses between two strings.
Note that if you do not need the edit path, PHP has a built-in [levenshtein()](http://php.net/manual/en/function.levenshtein.php) function.## Features
- UTF-8-ready.
- Full edit progresses information.## Installation
```bash
$ composer require jfcherng/php-levenshtein-distance
```## Example
See `demo.php`.
```php
calculate($old, $new);// this is the same but using an internal singleton
$results = LD::staticCalculate(
$old, // old string
$new, // new string
true, // calculate edit progresses?
// progress options
LD::PROGRESS_OP_AS_STRING | LD::PROGRESS_PATCH_MODE
);// [
// 'distance' => 5,
// 'progresses' => [
// ['ins', 8, '!', 1],
// ['rep', 7, '组', 1],
// ['cpy', 6, '模', 1],
// ['rep', 5, '语', 1],
// ['rep', 4, '词', 1],
// ['cpy', 3, '代', 1],
// ['cpy', 2, '取', 1],
// ['rep', 1, '订', 1],
// ['cpy', 0, '自', 1],
// ],
// ]
var_dump($results);$results = LD::staticCalculate(
$old, // old string
$new, // new string
true, // calculate edit progresses?
// progress options
LD::PROGRESS_OP_AS_STRING | LD::PROGRESS_PATCH_MODE | LD::PROGRESS_MERGE_NEIGHBOR
);// [
// 'distance' => 5,
// 'progresses' => [
// ['ins', 8, '!', 1],
// ['rep', 7, '组', 1],
// ['cpy', 6, '模', 1],
// ['rep', 4, '词语', 2],
// ['cpy', 2, '取代', 2],
// ['rep', 1, '订', 1],
// ['cpy', 0, '自', 1],
// ],
// ]
var_dump($results);
```## Progress Options
- `LD::PROGRESS_NO_COPY`: Do not include `COPY` operations in the progresses.
- `LD::PROGRESS_MERGE_NEIGHBOR`: Merge neighbor progresses if possible.
- `LD::PROGRESS_OP_AS_STRING`: Convert the operation in progresses from int to string.
- `LD::PROGRESS_PATCH_MODE`: Replace the new edit position with the corresponding string.## Returned `progresses`
1. The operation.
1. The edit position for the new string.
1. The edit position for the old string.
Or the corresponding string if `LD::PROGRESS_PATCH_MODE` is used.
1. The edit length.