levenshtein()
Our article is about the PHP function levenshtein(), which is used to calculate the Levenshtein distance between two strings. This function is useful for
The levenshtein() function calculates the Levenshtein distance between two strings — the minimum number of single-character edits (insertions, deletions, or substitutions) needed to turn one string into the other. A smaller distance means the strings are more alike, so levenshtein() is the go-to tool for fuzzy matching: spell-checkers, "did you mean…?" suggestions, deduplicating near-identical records, and ranking search results by closeness.
This chapter covers the syntax, the optional cost weights, how it differs from related functions, common gotchas, and runnable examples.
Syntax
levenshtein(string $string1, string $string2): intOr, with custom edit costs:
levenshtein(
string $string1,
string $string2,
int $insertion_cost,
int $replacement_cost,
int $deletion_cost
): intParameters
$string1— the first string to compare.$string2— the second string to compare.$insertion_cost(optional) — cost of inserting a character. Default1.$replacement_cost(optional) — cost of replacing a character. Default1.$deletion_cost(optional) — cost of deleting a character. Default1.
The function returns the Levenshtein distance as an int. With the default weights, that distance is symmetric — levenshtein($a, $b) equals levenshtein($b, $a).
The cost arguments come in a group of three. There is no single "max length" argument — if you only need the plain distance, call levenshtein() with just the two strings.
Basic example
The output of this code is:
4The function returns 4: turning "Hello" into "World" takes four substitutions (H→W, e→o, l→r, o→d); only the second l stays in place.
How the distance is built
Each edit operation counts as one step (with default weights). The classic textbook pair "kitten" → "sitting" needs three edits:
<?php
echo levenshtein("kitten", "sitting"); // 3
// k → s (substitution)
// e → i (substitution)
// (append) g (insertion)
?>Output:
3levenshtein() is case-sensitive
Different letter casing counts as an edit, which often surprises people:
<?php
echo levenshtein("Hello", "hello"), "\n"; // 1 (H vs h)
echo levenshtein(strtolower("Hello"), strtolower("hello")), "\n"; // 0
?>Output:
1
0For case-insensitive comparison, normalize both strings with strtolower() (or mb_strtolower() for multibyte text) before calling levenshtein().
Weighting edits differently
When insertions, deletions, and replacements should not all cost the same, pass the three cost arguments. Here a deletion is made expensive:
<?php
// $insertion_cost = 1, $replacement_cost = 1, $deletion_cost = 5
echo levenshtein("cats", "cat", 1, 1, 5); // 5
?>Output:
5Removing the trailing s is a single deletion, but at a cost of 5 the reported distance is 5. This is handy when one kind of typo should be penalized more than another.
Practical use: "did you mean?" suggestions
A common real-world job is finding the closest known word to a user's input:
<?php
$input = "comit";
$dictionary = ["commit", "command", "comment", "compile"];
$best = null;
$bestDistance = PHP_INT_MAX;
foreach ($dictionary as $word) {
$d = levenshtein($input, $word);
if ($d < $bestDistance) {
$bestDistance = $d;
$best = $word;
}
}
echo "Did you mean: {$best}? (distance {$bestDistance})";
?>Output:
Did you mean: commit? (distance 1)Gotchas
- Bytes, not characters.
levenshtein()works on single bytes, so multibyte UTF-8 characters (accents, emoji, non-Latin scripts) can be miscounted. For accurate results with such text, transliterate or compare normalized ASCII. - Long strings cost memory/time. Complexity is roughly proportional to the product of the two string lengths, so avoid it on very large inputs.
- Case and whitespace count. Trim and lowercase first if those differences should be ignored.
Related functions
similar_text()— returns the number of matching characters (and an optional percentage) instead of an edit distance.soundex()andmetaphone()— compare strings by how they sound rather than how they are spelled.strcmp()— a strict, binary-safe comparison that only tells you whether strings are equal or which sorts first.