(No version information available, might only be in Git)
grapheme_levenshtein — Calculate Levenshtein distance between two strings in grapheme units
Yordamsal kullanım
$string1,$string2,$insertion_cost = 1,$replacement_cost = 1,$deletion_cost = 1,$locale = ""
The Levenshtein distance is defined as the minimal number of
grapheme clusters that have to be replaced, inserted, or deleted to transform
string1 into string2.
The complexity of the algorithm is O(m*n),
where n and m are the
length of string1 and
string2 in grapheme units.
Unlike levenshtein(), which operates on bytes, this
function counts Unicode grapheme clusters, so composed and decomposed
forms of the same character (e.g. U+00E9 and
U+0065 U+0301, both representing é)
are treated as equivalent and have a distance of zero.
If insertion_cost, replacement_cost
and/or deletion_cost are unequal to 1,
the algorithm adapts to choose the cheapest transforms.
For example, if $insertion_cost + $deletion_cost < $replacement_cost,
no replacements will be done, but rather inserts and deletions instead.
string1One of the strings being evaluated for Levenshtein distance. Must be valid UTF-8.
string2One of the strings being evaluated for Levenshtein distance. Must be valid UTF-8.
insertion_cost
Defines the cost of insertion. Must be greater than 0.
replacement_cost
Defines the cost of replacement. Must be greater than 0.
deletion_cost
Defines the cost of deletion. Must be greater than 0.
localeLocale to use.
Returns the Levenshtein distance between the two strings, measured in
grapheme units, or false on failure. Use
intl_get_error_message() to retrieve details about
the failure.
Throws a ValueError if
insertion_cost, replacement_cost,
or deletion_cost is less than or equal to
0.
Returns false and sets an intl error if either input string is not
valid UTF-8, if locale is not a valid locale
identifier, or if an internal ICU error occurs.
| Sürüm: | Açıklama |
|---|---|
| 8.5.0 | This function has been added. |
Örnek 1 grapheme_levenshtein() example
<?php
// Composed form (NFC): U+00E9 LATIN SMALL LETTER E WITH ACUTE
$e_composed = "\u{00E9}";
// Decomposed form (NFD): U+0065 + U+0301 (e + combining acute accent)
$e_decomposed = "\u{0065}\u{0301}";
// grapheme_levenshtein treats them as the same grapheme cluster
var_dump(grapheme_levenshtein($e_composed, $e_decomposed));
// levenshtein() operates on bytes and sees them as different
var_dump(levenshtein($e_composed, $e_decomposed));
?>Yukarıdaki örneğin çıktısı:
int(0) int(3)