大网数据广告活动
PHP 语言与运行手册

grapheme_levenshtein · Calculate Levenshtein distance between two strings in grapheme units

· 52CE 我爱测

grapheme_levenshtein — Calculate Levenshtein distance between two strings in grapheme units

grapheme_levenshtein

(PHP 8 >= 8.5.0)

grapheme_levenshtein — Calculate Levenshtein distance between two strings in grapheme units

说明

过程化风格

function grapheme_levenshtein(
    string $string1,
    string $string2,
    int $insertion_cost = 1,
    int $replacement_cost = 1,
    int $deletion_cost = 1,
    string $locale = ""
): int|false

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.

参数

string1

One of the strings being evaluated for Levenshtein distance. Must be valid UTF-8.

string2

One 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.

locale

要使用的区域设置。

返回值

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.

更新日志

版本 说明
8.5.0 This function has been added.

示例

示例 #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));

?>

以上示例会输出:

int(0)
int(3)

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。