Calculate Levenshtein distance between two strings


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $dictionary = array(
  2. "php", "javascript", "css"
  3. );
  4.  
  5. $word = "japhp";
  6.  
  7. $best_match = $dictionary[0];
  8. $match_value = levenshtein($dictionary[0], $word);
  9.  
  10. foreach($dictionary as $w) {
  11. $value = levenshtein($word, $w);
  12. if( $value < $match_value ) {
  13. $best_match = $w;
  14. $match_value = $value;
  15. }
  16. }
  17.  
  18. echo "Did you mean the '$best_match' category?";

URL: http://uk.php.net/levenshtein

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.