PHP, complex string comparison
Disclaimer: I’m not familiar with PHP, using it super rarely if somebody really needs my help 🙂
Some time ago I had to write a PHP function for one popular CMS for searching products in the database.
Main target – find correct products if user is doing typos in the input, for example:
Find ‘testers are cool’ if user enters ‘Tecteps ur kyl’.
I like SQL and know about one cool function – DIFFERENCE(string1, string2), it uses SOUNDEX function for first and second strings and than compares theirs phonems. DIFFERENCE has 5 levels of alikness: 0, 1, 2, 3, 4, where 0 – completely different strings are passed. Very useful and easy to use. But trick is that MySQL doesn’t have this function xD And I have already said, that I will complete this difficult search in few hours xDD
But, PHP has a really cool function:
similar_text($string1, $string2, $percentage);
I have compared similar_text function to the (http://www.php.net/manual/en/function.levenshtein.php)
int levenshtein ( string $str1 , string $str2 );
But levenshtein is not so flexible and useful, it is useful for typos like:
rabir or rabbbbit -> rabbit
similar_text computes in percentages how string2 is alike with string1! And example I have gave above will be found with percentage > 40% 🙂
So you can simply process string data with this function for searching alike strings if user is doing typos. I have tested this method for a hundreds of examples – working just fine!
Happy searching (: