levenshtein() – PHP String Functions

Syntax :

levenshtein ( string1, string2)
OR
levenshtein ( string1, string2, char_ins , char_rep , char_del)

Description :

It’s an inbuilt function of PHP. levenshtein() function will calculate Levenshtein distance between two strings.

Note: The function is not case-sensitive.
Note:  This function is faster than the similar_text() function.

Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description
string1, Required, String, Main string to check into.
string2, Required, String, Main string to check into.
char_ins, Optional, Integer, The cost of inserting a character Default is 1.
char_rep, OptionalInteger, The cost of replacing a character Default is 1.
char_del, OptionalInteger, The cost of deleting a character Default is 1.
[/table]


Output :

This function returns the Levenshtein-Distance between the two argument strings or -1, if one of the argument strings is longer than the limit of 255 characters.


Related articles : stripos(), strpos(), strripos().


levenshtein() – PHP Functions Example 1 :
<?php
echo levenshtein("Hello From Tutorialmines.net"," From Tutor");
echo "<br>";
echo levenshtein("Hello From Tutorialmines.net","From Tutorialmines.net",2,5,3);
?>

Output of above code in the browser is as below:

5
18

You may also like...