TechniqueUse PHP's soundex() function to calculate the soundex index of a string. $soundex_str = soundex ($str); CommentsCalculating the soundex equivalent of a string is very helpful in fuzzy queries, or queries where you have to be a little forgiving of the user . The soundex() function returns a key for a certain word. For example, the key for Euler is E460. Words that sound similar, such as Ellery, also have the same key. Therefore, while doing queries, you could search for all words that have the same or a similar soundex key to the word that your user entered. The particular soundex function used by PHP is one described by Donald Knuth in The Art Of Computer Programming, vol. 3: Sorting and Searching, Addison-Wesley (1973), pp. 391 “392. Finally, here are a few examples from the documentation: soundex("Euler") == soundex("Ellery") == 'E460'; soundex("Gauss") == soundex("Ghosh") == 'G200'; soundex("Knuth") == soundex("Kant") == 'K530'; soundex("Lloyd") == soundex("Ladd") == 'L300'; soundex("Lukasiewicz") == soundex("Lissajous") == 'L222'; Sometimes you want to find out how similar two strings are. For that, PHP has the similar_text() function. It takes two strings and an optional variable that will be used to store the similarity percentage, and it returns the number of matching characters in both strings. Here are some examples of similar_text() usage: $num_match = similar_text("Euler", "Ellery", $similarity); /* num_match is 4, $similarity is 72.7% */ $num_match = similar_text("Gauss", "Ghosh", $similarity); /* num_match is 2, $similarity is 40% */ $num_match = similar_text("conscience", "consciousness", $similarity); /* num_match is 7, $similarity is 60.9% */ similar_text() can be slow on very long strings, so use it carefully . |