levenshtein()
Oooh! I don’t suppose anyone has a “reblog into fogbugz” greasemonkey script handy?
levenshtein() tells you how “far” away two words are. It will return the minimum number of inserts, replaces and deletions needed to transform one string to the other. Take, for example, the following code:
$dictionary = array(
"php", "javascript", "css"
);
$word = "japhp";
$best_match = $dictionary[0];
$match_value = levenshtein($dictionary[0], $word);
foreach($dictionary as $w) {
$value = levenshtein($word, $w);
if( $value < $match_value ) {
$best_match = $w;
$match_value = $value;
}
}
echo "Did you mean the '$best_match' category?";In this case, the user has been asked to provide a category name. They provided “japhp”, which is invalid. Since this is likely to be a typing error, the code above will make a suggestion (”Did you mean the ‘php’ category?”).
Awesome, right? A few more things you probably didn’t know here.
-
david liked this
-
webmarc reblogged this from hellaposer and added:
Oooh! I don’t suppose anyone...“reblog into fogbugz” greasemonkey script handy?
-
webmarc liked this
-
dears reblogged this from hellaposer
-
ben liked this
-
airport liked this
-
topherchris liked this
-
hellaposer posted this