PHP | levenshtein() Function
The levenshtein() function is an inbuilt function in PHP. The levenshtein() function is used to calculate the levenshtein distance between two strings. The Levenshtein distance between two strings is defined as the minimum number of characters needed to insert, delete or replace in a given string $string1 to transform it to a string $string2.
Syntax:
int levenshtein($str1, $str2)
Example:
Input: $str1 = 'GeeksforGeeks', $str2 = 'Geeksfor' Output: 5 Input: $str1 = 'Computer Science Portal', $str2 = 'Computer Portal' Output: 8
Parameters: The levenshtein() function accepts two parameters, both of the parameters are compulsory:
- $str1: This is a required parameter which specifies the string to be transformed to another.
- $str2: This is also a required parameter which specifies the string in which the first string($str1) needed to be trasformed.
Return Value: The levenshtein() function returns an integral value which is the levenshtein distance otherwise -1, if one of the arguments exceeds the limit of 255 characters.
Below programs illustrate The levenshtein() function in PHP:
Program 1:
<?php // PHP code to find levenshtein distance // between $str1 and $str2 $str1 = 'abc'; $str2 = 'aef'; print_r(levenshtein($str1, $str2)); ?> |
Output:
2
Program 2:
<?php // PHP code to find levenshtein distance // between $str1 and $str2 $str1 = 'Hello World'; $str2 = 'Hello d'; print_r(levenshtein($str1, $str2)); ?> |
Output:
4
Program 3:
<?php // PHP code to find levenshtein distance // between $str1 and $str2 $str1 = 'Computer Science Portal'; $str2 = 'Computer Portal'; print_r(levenshtein($str1, $str2)); ?> |
Output:
8
Reference:
http://php.net/manual/en/function.levenshtein.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- p5.js | nfs() Function
- p5.js | sq() function
- CSS | var() Function
- p5.js | abs() function
- p5.js | nf() Function
- p5.js | nfp() Function
- D3.js | d3.map.set() Function
- PHP Ds\Set get() Function
- D3.js | d3.set.add() Function
- D3.js | d3.min() function
- PHP | pi( ) Function
- CSS | url() Function
- PHP | each() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



