Ce document est aussi disponible en français
levenshtein() is a special function for string manipulation. It calculates a distance between two strings. This is an integer that quantify the difference between two strings. The Levenshtein distance is defined as the smallest number of character that must be replace, inserted or deleted to turn one string into another.
This function evaluates string differences and return a number.
Such a function is useful in a range of applications : filtering text, spelling mistakes, string comparison, typo detections, etc.Similarly, there is the similar_text() function, which calculates the number of identical chars in each strings, returning a similarity percentage, by taking into account the length of the strings.
Example 1 - Avoiding too much UPPERCASE in a string
<?php $subject = 'ThIS stRIng Has wAy tOO MANY upPer caSe charActerS'; $subject = str_replace(' ','',$subject); $pourcentage = levenshtein ($subject, strtolower($subject))*100/strlen($subject); echo 'There is ' . $pourcentage . '% upper case characters in this string.'; ?>
Example 2 - Supposer une faute d'orthographe
<?php if (levenshtein($sqlResult['name'], $_POST['username']) <2 ){ echo 'Another user with a similar name already exists.'; } ?><strong> </strong>
Example 3 - Finding similar yet differents strings
<?php $word1 = 'gatcha!'; $word2 = 'got you!'; $similitude = levenshtein(metaphone($word1),metaphone($word2)); if ($similitude < 2){ echo $word1 .' and '. $word2 .' seems to be close enough.'; ?>
Example 4 - Too similar password (when changing)
<?php
similar_text($new_password, $old_password, $percent1);
similar_text($new_password, $userid, $percent2);
if (($percent1 > 80) || ($percent2 > 80)) {
echo"Your new password should be much more different from the old one.");
}
?>
Keep in mind
- Levenshtein() should not be used on large strings. Beyond 255 chars, it will return -1.
- Levenshtein() and similar_text() are often used in conjunction with soundex(), for phonetic comparison.
- Levenshtein() is faster than similar_text(), thanks to its less complex algorithm.
Vous pouvez ajouter votre commentaire! |


