The levenshtein() function in PHP is a fairly uncommon function and is unknown to most coders, however, this function is a very cool and powerful function that has many uses and is VERY handy! Today, I am going to show you how to use it and what it can do by making a simple, yet powerful, word suggestion tool.

First of all, the way this function works, is that it takes one string and compares it to another and finds the shortest amount of changes necessary to make them similar. Now while this may sound both confusing and unnecessary, it is quite extraordinary and very useful.

This is the script we are going to make today, it will take a word as an input (spelled wrong) and tell you the closest matches to your input, thereby acting as a word suggestion tool or a spell checker tool.

The Script:

PHP:
  1. <?php
  2.  
  3. if ($REQUEST_METHOD == "post") {
  4.  
  5. $input = $_POST['word'];
  6.  
  7. $dict = array('dog', 'fish', 'cat', 'parrot', 'humming bird', 'snake', 'rabbit', 'frog', 'turtle', 'bee', 'ant', 'scorpion', 'wolf', 'bear', 'panda', 'anaconda', 'llama', 'cow', 'sheep', 'horse', 'goat');
  8.  
  9. $shortest = -1;
  10.  
  11. foreach ($dict as $word) {
  12.  
  13.      $search = levenshtein($input, $word);
  14.  
  15.      if ($search == 0) {
  16.           $suggest = $word;
  17.           $shortest = 0;
  18.  
  19.           break;
  20.      }
  21.      
  22.      if ($search <= $shortest || $shortest <0) {
  23.           $suggest = $word;
  24.           $shortest = $search;
  25.      }
  26. }
  27.  
  28.      Input: $input <br />
  29.      Closest Match: $suggest
  30. ";
  31.  
  32. }
  33.  
  34. ?>
  35.  
  36. <html>
  37. <head>
  38. <title>Levenshtein Example</title>
  39. </head>
  40. <body>
  41. <form action="<?php echo($PHP_SELF); ?>" method="POST">
  42.      Word: <input type="text" name="word" /><br />
  43.      <input type="submit" value="Find Suggestion" />
  44. </form>
  45. </body>
  46. </html>

That is the script in it's entirety. Now I will explain the code and what it does.

First of all it displays a form in which the user inputs a word (preferably spelled wrong ;) ). Then upon submitting the form it will check the input against an array of words. When it finds the word that is the closest match to the input it echo's the results.

This code can easily be modified to check the input against a dictionary file, a database, or even an online dictionary (such as dictionary.com :D ). It has many possibilities and endless uses.

Feel free to comment on my script and any changes you make to it, I encourage you to take this script and make changes, play with it, and learn cool new ways to use the levenshtein() function!

Happy Coding and Good Luck!

3 Responses to “PHP’s Levenshtein Function”

  1. Noob

    gdfgdfgdfgdfgdf

    July 29th, 2008 | 3:19 pm
  2. smxwqonb cmri otmzxben sdgpumj iduf esplf lsgqx

    August 2nd, 2008 | 6:48 am
  3. Fajna stronka, bede tu wpadal czesciej, pozdro

    August 21st, 2008 | 1:45 pm

Leave a Reply