Home >>PHP String Functions >PHP levenshtein() Function
PHP levenshtein() function is used to get the levenshtein distance between two given input strings. The Levenshtein distance between two strings is the minimum number of characters we needed to insert, delete or replace in a given input string $string1 to transform it to another string $string2. By default, for each operation replace, insert, and delete we have equal cost “1” but we can also define the cost of each operation by passing the value of optional parameters $insert, $replace, and $delete.
Syntax :-
levenshtein($string1, $string2, $insert, $replace, $delete);
Parameter | Description |
---|---|
string1 | This is a required parameter. This parameter contains the first string to be compared. |
string2 | This is a required parameter. This parameter contains the second string to be compared. |
insert | This is an optional parameter. This parameter contains the cost of inserting a character. By default its value be 1. |
replace | This is an optional parameter. This parameter contains the cost of replacing a character. By default its value be 1. |
delete | This is an optional parameter. This parameter contains the cost of deleting a character. By default its value be 1. |
Here is an example of levenshtein() function in PHP:
<html> <body> <?php echo levenshtein("PHP!!","PHP"); //deleting the characters ?> </body> </html>
Here is an another example of levenshtein() function in PHP:
<html> <body> <?php echo levenshtein("PHP","PHPTPOINT"); //inserting the characters ?> </body> </html>
Here is an another example of levenshtein() function in PHP passing the value of optional parameters:
<html> <body> <?php echo levenshtein("PHP","PHPTPOINT"); echo "<br>"; echo levenshtein("PHPTPOINT","PHP",10,20,30); ?> </body> </html>