PHP levenshtein() Function
In this chapter you will learn:
- Definition for PHP levenshtein() Function
- Syntax for PHP levenshtein() Function
- Parameter for PHP levenshtein() Function
- Return for PHP levenshtein() Function
- Example - Calculate the Levenshtein distance between two strings
Definition
The levenshtein() function returns the Levenshtein distance between two strings case-insensitive.
The Levenshtein distance is the number of characters to replace, insert or delete to transform string1 into string2.
By default, PHP gives replace, insert, and delete equal weight. We can define the cost of each operation by setting the optional insert, replace, and delete parameters.
Syntax
PHP levenshtein() Function has the following syntax.
levenshtein(string1,string2,insert,replace,delete)
Parameter
Parameter | Is Required | Description |
---|---|---|
string1 | Required. | First string to compare |
string2 | Required. | Second string to compare |
insert | Optional. | The cost of inserting a character. Default is 1 |
replace | Optional. | The cost of replacing a character. Default is 1 |
delete | Optional. | The cost of deleting a character. Default is 1 |
Return
PHP levenshtein() function Returns the Levenshtein distance between the two argument strings or -1, if one of the strings exceeds 255 characters.
Example
Calculate the Levenshtein distance between two strings:
<?php//from j a v a 2 s .co m
echo levenshtein("Hello World","ello World from java2s.com");
echo "\n";
echo levenshtein("Hello World","ello World from java2s.com",10,20,30);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP localeconv() Function
- Syntax for PHP localeconv() Function
- Parameter for PHP localeconv() Function
- Return for PHP localeconv() Function
- Example - Find the United States locale numeric formatting information: