How to use levenshtein in PHP

In this article I will explain how the levenshtein() function can be used in PHP.
  • 1488
evenshtein() function in PHP
  • The levenshtein() function is used to return the Levenshtein distance between two strings or argument.
  • In levenshtein() function, the Levenshtein distance is the number of characters you have to replace, insert or delete to transform string1 into string2.
  • In levenshtein() function, one of the argument strings is longer than the limit of 255 characters.
  • In levenshtein() function, the complexity of the algorithm is O(m*n), where n and m are the length of str1 and str2.

Syntax

levenshtein(string1,string2,insert,replace,delete)

Parameter

  • string1 separator is required parameter. First string to compare.
  • string2 array is required parameter. Second string to compare.
  • insert array is optional parameter. The cost of inserting a character. the insert default value is 1.
  • replace array is optional parameter. The cost of replacing a character. the replace default value is 1.
  • delete array is optional parameter. The cost of deleting a character. the delete default value is 1.

Example

The following example show to how the levenshtein() function can be used in PHP.

<html>

<body>

<h3 style="color: green;">levenshtein() function example in PHP</h3>

    <?php

    echo "Levenshtein distance between two strings is : ". levenshtein("Welcome to","c-sharpcorner");

    echo "<br />";

    echo "Levenshtein distance between two strings is : ".levenshtein("Hello ","c-sharpcorner    

     user",10,20,30);

    ?>

</body>

</html>

 

Output

levenshtein-php.gif
You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.