PHPackages                             designbycode/levenshtein-distance - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. designbycode/levenshtein-distance

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

designbycode/levenshtein-distance
=================================

The LevenshteinDistance class provides a method to calculate the Levenshtein distance between two strings. The Levenshtein distance is a measure of the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into the other.

v1.0.1(1y ago)1110[2 PRs](https://github.com/designbycode/levenshtein-distance/pulls)1MITPHPPHP ^8.1|^8.2|^8.3

Since Jul 20Pushed 1y agoCompare

[ Source](https://github.com/designbycode/levenshtein-distance)[ Packagist](https://packagist.org/packages/designbycode/levenshtein-distance)[ Docs](https://github.com/designbycode/levenshtein-distance)[ GitHub Sponsors](https://github.com/designbycode)[ RSS](/packages/designbycode-levenshtein-distance/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (2)Dependencies (3)Versions (5)Used By (1)

Levenshtein Distance
====================

[](#levenshtein-distance)

[![Latest Version on Packagist](https://camo.githubusercontent.com/35534088d2550059f2d32fea5de49f3c44faec2755991b17bbd098fef721e537/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64657369676e6279636f64652f6c6576656e73687465696e2d64697374616e63652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/designbycode/levenshtein-distance)[![Tests](https://camo.githubusercontent.com/25f79af5fc610102dd71b2c050aea77089b95a67444a468414ce2c26767210ba/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f64657369676e6279636f64652f6c6576656e73687465696e2d64697374616e63652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/designbycode/levenshtein-distance/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/56f71cd974b86d2dd52dcfacf746e9ccdf4609327493e1a016883cdeb96f1c13/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64657369676e6279636f64652f6c6576656e73687465696e2d64697374616e63652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/designbycode/levenshtein-distance)

The LevenshteinDistance class provides a method to calculate the Levenshtein distance between two strings. The Levenshtein distance is a measure of the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into the other.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require designbycode/levenshtein-distance
```

Method: calculate
-----------------

[](#method-calculate)

### Parameters

[](#parameters)

- mixed $str1: The first string.
- mixed $str2: The second string.
-

### Return Value

[](#return-value)

- int: The Levenshtein distance between the two strings.

### Description

[](#description)

- Calculates the Levenshtein distance between two strings. The method throws a TypeError if either of the input parameters is not a string.

Use Cases
---------

[](#use-cases)

1. Spell Checking: Calculate the Levenshtein distance between a user's input and a list of known words to suggest corrections.
2. Text Similarity: Measure the similarity between two pieces of text by calculating the Levenshtein distance.
3. Data Validation: Verify the correctness of user input by calculating the Levenshtein distance between the input and a known valid value.

Usage
-----

[](#usage)

### Example 1: Calculating the Levenshtein distance between two strings

[](#example-1-calculating-the-levenshtein-distance-between-two-strings)

```
$str1 = 'kitten';
$str2 = 'sitting';
$distance = LevenshteinDistance::calculate($str1, $str2);
echo "Levenshtein distance: $distance"; // Output: 3
```

### Example 2: Handling non-string input

[](#example-2-handling-non-string-input)

```
$str1 = 'hello';
$nonString = 123;
try {
    LevenshteinDistance::calculate($str1, $nonString);
} catch (TypeError $e) {
    echo "Error: " . $e->getMessage(); // Output: Argument 2 passed to LevenshteinDistance::calculate() must be of the type string
}
```

### Example 3: Using Levenshtein distance for spell checking

[](#example-3-using-levenshtein-distance-for-spell-checking)

```
$userInput = 'teh';
$knownWords = ['the', 'tea', 'ten'];
$minDistance = PHP_INT_MAX;
$closestWord = '';

foreach ($knownWords as $word) {
    $distance = LevenshteinDistance::calculate($userInput, $word);
    if ($distance < $minDistance) {
        $minDistance = $distance;
        $closestWord = $word;
    }
}

echo "Did you mean: $closestWord"; // Output: Did you mean: the
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Claude Myburgh](https://github.com/claudemyburgh)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

714d ago

PHP version history (2 changes)v1.0.0PHP ^8.1

v1.0.1PHP ^8.1|^8.2|^8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6057076?v=4)[Claude Myburgh](/maintainers/claudemyburgh)[@claudemyburgh](https://github.com/claudemyburgh)

---

Top Contributors

[![claudemyburgh](https://avatars.githubusercontent.com/u/6057076?v=4)](https://github.com/claudemyburgh "claudemyburgh (4 commits)")

---

Tags

levenshteinlevenshtein-distancedesignbycode

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/designbycode-levenshtein-distance/health.svg)

```
[![Health](https://phpackages.com/badges/designbycode-levenshtein-distance/health.svg)](https://phpackages.com/packages/designbycode-levenshtein-distance)
```

###  Alternatives

[atomescrochus/laravel-string-similarities

Compare two string and get a similarity percentage

70180.6k2](/packages/atomescrochus-laravel-string-similarities)[oefenweb/damerau-levenshtein

Get text similarity level with Damerau-Levenshtein distance

42287.0k4](/packages/oefenweb-damerau-levenshtein)[edgaras/strsim

Collection of string similarity and distance algorithms in PHP including Levenshtein, Damerau-Levenshtein, Jaro-Winkler, and more

2857.3k6](/packages/edgaras-strsim)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
