# RFC: Add mb_levenshtein function

- Status: Declined
- Authors: Yuya Hamada, youkidearitai@gmail.com
- Date: 2024-09-25
- Version: 0.1
- Original: https://wiki.php.net/rfc/mb_levenshtein
- Page: https://externals.io/rfc/mb_levenshtein
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Add mb_levenshtein function (closed)

Yes: 1 · No: 5

Voters: cschneid (No), galvao (No), nielsdos (No), reywob (Yes), thorstenr (No), timwolla (No)

## Discussions on the mailing list

- [\[DISCUSSION\] Multibyte for levenshtein function](https://externals.io/message/125674.md) — started on 2024-09-25, 4 messages
- [\[RFC\] Add mb_levenshtein function](https://externals.io/message/126307.md) — started on 2025-02-06, 3 messages
- [\[RFC\]\[VOTE\] Add mb_levenshtein function](https://externals.io/message/126471.md) — started on 2025-02-21, 6 messages

---

## Introduction

Multibyte levenshtein distances have feature requests in the past. Therefore, we would like to create the mb\_levenshtein function to implement this.

ref: <https://github.com/php/php-src/issues/10180>

# Levenshtein distance difference mb\_levenshtein vs grapheme\_levenshtein

The mb\_levenshtein function is the Levenshtein distance in code points. This is useful for comparing Unicode code points. For example, this can be used to compare concatenated characters.

```
var_dump(mb_levenshtein("\u{0065}\u{0301}", "\u{00e9}")); // "é" result is 1.
```

Surely, There are times when I want to consider this to be the same. In that case, I will propose grapheme\_levenshtein separately.

## Proposal

Add mb\_levenshtein function.

```
function mb_levenshtein(string $string1, string $string2, int $insertion_cost = 1, int $replacement_cost = 1, int $deletion_cost = 1, ?string $encoding = null): int {}
```

## Backward Incompatible Changes

This could break a function existing in userland with the same name.

## Proposed PHP Version(s)

PHP 8.5

## RFC Impact

### To SAPIs

To SAPIs Will add the aforementioned functions to all PHP environments.

### To Existing Extensions

Adds mb\_levenshtein() to the mbstring extension.

### To Opcache

No effect.

### New Constants

No new constants.

### php.ini Defaults

No changed php.ini settings.

## Open Issues

<https://github.com/php/php-src/issues/10180>

## Future Scope

This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC.

## Proposed Voting Choices

Include these so readers know where you are heading and can discuss the proposed voting options.

## Voting

**Vote: Add mb\_levenshtein function** — Yes: 1 · No: 5 (closed)

## Implementation

<https://github.com/php/php-src/pull/16043>

## References

Userland implementation is here:

- [https://github.com/KEINOS/mb\_levenshtein](https://github.com/KEINOS/mb_levenshtein)

## Rejected Features

Keep this updated with features that were discussed on the mail lists.
