Computer >> Computer tutorials >  >> Programming >> PHP

levenshtein() function in PHP


The levenshtein() function is used calculate Levenshtein distance between two strings. The Levenshtein distance is the number of characters you have to replace, insert or delete to transform first string into second string. This function is not case-sensitive.

Syntax

levenshtein(str1, str2, insert, replace, delete)

Parameters

  • str1 − The first string to compare

  • str2 − The second string to compare

  • insert − Cost of inserting a character

  • replace − Cost of replacing a character

  • delete − Cost of deleting a character

Return

The levenshtein() function returns the Levenshtein distance between the two argument strings. It returns-1, if one of the strings exceeds 255 characters

Example

The following is an example −

<?php
   echo levenshtein("Welcome","elcome");
?>

Output

1