Saturday, 27 September 2014

levenshtein in PHP

PHP levenshtein() function is utilized to calculates the Levenshtein distance between twostrings.

The PHP levenshtein() function distance is the number of characters you need to replace, insert or delete to change string1 into string2.

Syntax:

Parameters Description:
levenshtein(string1,string2,string3,insert,replace,delete)

string1 : Required. Defines First string
string2 : Required. Defines Second string
insert : Optional. Defines the cost of insertion. Default is one.
replace : Optional. Defines the cost of replacement. Default is one.
delete : Optional. Defines the cost of deletion . Default is one.
Note : The PHP levenshtein() function is incase-sensitive and returns -1 if any of string exceeds 255 characters.
Note : The PHP levenshtein() function is speedier than similar_text() function. On the other hand, similar_text() function will give more correct result.

Example:
<?php
echo levenshtein("My name is sam","name is sam"); 
echo "<br />";
echo levenshtein("My name is sam","name is sam",5,20,10);
?>
O/P:
3
30

0 comments:

Post a Comment