PHP developers sometimes need to measure the edit distance between two strings. This comes up when preparing data, displaying results, or integrating information with other services.
The levenshtein() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.
What Is levenshtein() in PHP?
Measure the edit distance between two strings.
Its syntax is:
levenshtein(string $string1, string $string2, int $insertion_cost = 1, int $replacement_cost = 1, int $deletion_cost = 1): int
The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.
Important: Built-in distance compares bytes, not Unicode grapheme clusters. It is useful for suggestions, not proof that two words are equivalent.
Parameters:
$string1— A named input or configuration argument; use the declared type and optional default shown above.$string2— A named input or configuration argument; use the declared type and optional default shown above.$insertion_cost— A named input or configuration argument; use the declared type and optional default shown above.$replacement_cost— A named input or configuration argument; use the declared type and optional default shown above.$deletion_cost— A named input or configuration argument; use the declared type and optional default shown above.
Example 1: Basic Usage
This first example uses a small input to demonstrate the main result of levenshtein().
<?php
echo levenshtein("cat", "cut");
Output:
1
The output demonstrates the primary operation: measure the edit distance between two strings.
Example 2: Understanding Another Case
This second example changes the input or option to reveal a different behavior of levenshtein().
<?php
echo levenshtein("book", "books");
Output:
1
Compared with the first example, this one illustrates the same operation with a different input shape or option. Built-in distance compares bytes, not Unicode grapheme clusters. It is useful for suggestions, not proof that two words are equivalent.
Practical Example: A Real Application Scenario
Here is a practical use of levenshtein() with realistic sample information. Adapt the values to match your application.
<?php
$typed = "notebok"; $candidate = "notebook"; echo "Edits needed: " . levenshtein($typed, $candidate);
Output:
Edits needed: 1
This is a useful starting point, but production code should check errors and validate values before performing the operation. Built-in distance compares bytes, not Unicode grapheme clusters. It is useful for suggestions, not proof that two words are equivalent.
Important Behavior and Edge Cases
Built-in distance compares bytes, not Unicode grapheme clusters. It is useful for suggestions, not proof that two words are equivalent.
- Input: Use the expected PHP types and validate user-provided values.
- Result: Check the return value separately from any modifications to the input.
- Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
levenshtein() | Measure the edit distance between two strings. |
| Related functions | similar_text() calculates similarity as a different measure; strcmp() compares ordering. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing bytes with characters | Built-in distance compares bytes, not Unicode grapheme clusters. It is useful for suggestions, not proof that two words are equivalent. |
| Assuming a similarity score proves identity | Similarity estimates are not reliable for authorization or exact matching. |
| Using a different text operation without checking its semantics | similar_text() calculates similarity as a different measure; strcmp() compares ordering. |
Key Takeaways
levenshtein()lets you measure the edit distance between two strings.- Built-in distance compares bytes, not Unicode grapheme clusters. It is useful for suggestions, not proof that two words are equivalent.
- similar_text() calculates similarity as a different measure; strcmp() compares ordering.
- Check input, return values, and environmental requirements when adapting the examples.
levenshtein() provides a focused PHP operation for measure the edit distance between two strings. Start with the simple examples, then adapt the practical example with the correct checks for your project.