PHP levenshtein() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, levenshtein() counts how many single-character edits turn one string into another.

An edit is an insertion, a replacement, or a deletion. A smaller number means the strings are more alike.

Syntax

Call levenshtein() with the values you need. Start with this line.

PHP
$distance = levenshtein("kitten", "sitting");
CallWhat it does
levenshtein("kitten", "sitting")Returns 3.
levenshtein("php", "php")Returns 0 because the strings match.
levenshtein("cat", "cut", 1, 5, 1)Uses custom costs. Returns 2 because delete plus insert is cheaper than a replacement.

The comparison is case sensitive. In PHP 8 there is no length limit on the input strings.

Library declarations
PHP
levenshtein(string $string1, string $string2, int $insertion_cost = 1, int $replacement_cost = 1, int $deletion_cost = 1): int

Parameters

ParameterDescription
string1Required. The first string.
string2Required. The second string.
insertion_costOptional. Cost of inserting a character. Default is 1.
replacement_costOptional. Cost of replacing a character. Default is 1.
deletion_costOptional. Cost of deleting a character. Default is 1.

Return Value

Return ValueDescription
intThe Levenshtein distance between the two strings.

PHP Examples

Example 1 — Measure the Distance Between Two Words

The classic example: how far is kitten from sitting?

PHP
<?php
$distance = levenshtein("kitten", "sitting");

echo "Distance: " . $distance, PHP_EOL;

How It Works

  1. Pick two words. They are kitten and sitting.
  2. Replace k with s. That is the first edit.
  3. Replace e with i, then add g. That makes edits two and three.
  4. Read the result. The distance is 3.

Example 2 — Empty, Identical, and Case Differences

Check a few special inputs to see how the count behaves.

PHP
<?php
echo levenshtein("", "abc"), PHP_EOL;
echo levenshtein("hello", "hello"), PHP_EOL;
echo levenshtein("PHP", "php"), PHP_EOL;

How It Works

  1. Empty string. You need 3 insertions to build abc, so the result is 3.
  2. Identical strings. No edits are needed, so the result is 0.
  3. Different case. P and p are different bytes, so all 3 letters count.
  4. Tip. Use strtolower() on both strings first for a case-insensitive check.

Example 3 — Suggest the Closest Word

Loop through a word list and keep the one with the smallest distance.

PHP
<?php
$input = "banan";
$words = ["apple", "banana", "cherry"];
$best = "";
$bestScore = PHP_INT_MAX;

foreach ($words as $word) {
    $score = levenshtein($input, $word);
    if ($score < $bestScore) {
        $bestScore = $score;
        $best = $word;
    }
}

echo "Did you mean: " . $best, PHP_EOL;

How It Works

  1. Set up the list. The input is banan and there are three candidate words.
  2. Start high. PHP_INT_MAX makes sure the first score always wins.
  3. Compare each word. Keep the word whenever its score is lower.
  4. Print the best match. banana is only one insertion away.

Frequently Asked Questions

levenshtein() returns the minimum number of single-character insertions, replacements, and deletions needed to change one string into another.
int levenshtein(string $string1, string $string2, int $insertion_cost = 1, int $replacement_cost = 1, int $deletion_cost = 1). Only the two strings are required.
Yes. Pass the insertion, replacement, and deletion costs as the third, fourth, and fifth arguments. All three default to 1.
Yes. Uppercase and lowercase letters are different. Convert both strings with strtolower() first if you want to ignore case.
Use levenshtein() when you want an exact edit count, like a "did you mean" suggestion for typos. soundex() and metaphone() compare how words sound, and similar_text() gives a similarity score.

Next: localeconv()

localeconv() returns an array with number and currency formatting rules for the current lo.

localeconv() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful