Current Location: Home> Function Categories> levenshtein

levenshtein

Calculate the edit distance between two strings
Name:levenshtein
Category:String
Programming Language:php
One-line Description:Returns the Levenshtein distance between two strings.

Definition and usage

levenshtein() function returns the Levenshtein distance between two strings.

Levenshtein distance, also known as edit distance, refers to the minimum number of editing operations required between two strings to convert from one string to another. The licensed editing operation includes replacing one character with another character, inserting one character, and deleting one character.

By default, PHP gives the same weight for each operation (replace, insert, and delete). However, you can define the cost of each operation by setting optional insert, replace, delete parameters.

Note: levenshtein() function is case-insensitive.

Note: levenshtein() function is faster than similar_text() function. However, the similar_text() function gives you more accurate results with fewer required modifications.

Example

Calculate the Levenshtein distance between two strings:

 <?php
echo levenshtein ( "Hello World" , "ello World" ) ;
echo "<br>" ;
echo levenshtein ( "Hello World" , "ello World" , 10 , 20 , 30 ) ;
?>

Try it yourself

grammar

 levenshtein ( string1 , string2 , insert , replace , delete )
parameter describe
string1 Required. The first string that needs to be compared.
string2 Required. The second string that needs to be compared.
insert Optional. The cost of inserting a character. The default is 1.
replace Optional. The cost of replacing a character. The default is 1.
delete delete Optional. The cost of deleting a character. The default is 1.
Similar Functions
  • Escape strings using backslash addslashes

    addslashes

    Escapestringsusingba
  • Break string to a specified number of strings wordwrap

    wordwrap

    Breakstringtoaspecif
  • Set the first character of the string to uppercase ucfirst

    ucfirst

    Setthefirstcharacter
  • Set the first character of the string to lowercase lcfirst

    lcfirst

    Setthefirstcharacter
  • Parse strings into multiple variables parse_str

    parse_str

    Parsestringsintomult
  • Parses entered characters according to the specified format sscanf

    sscanf

    Parsesenteredcharact
  • Translate characters or replace substrings - convert specified characters strtr

    strtr

    Translatecharacterso
  • Convert hexadecimal string to binary string hex2bin

    hex2bin

    Converthexadecimalst
Popular Articles