I am looking for something that would give me a comparison relevance of two strings. For eg. If I have string1 and string2, I want to compare them and see how much percentage of these two strings match against one another. Like string1 is apprx 90% equal to string2. Something like this is what I am looking for in php. Please help.
<?php // returns the percentage of the string "similarity" function str_compare($str1, $str2) { $count = 0; $str1 = ereg_replace("[^a-z]", ' ', strtolower($str1)); while(strstr($str1, ' ')) { $str1 = str_replace(' ', ' ', $str1); } $str1 = explode(' ', $str1); $str2 = ereg_replace("[^a-z]", ' ', strtolower($str2)); while(strstr($str2, ' ')) { $str2 = str_replace(' ', ' ', $str2); } $str2 = explode(' ', $str2); if(count($str1)<count($str2)) { $tmp = $str1; $str1 = $str2; $str2 = $tmp; unset($tmp); } for($i=0; $i<count($str1); $i++) { if(in_array($str1[$i], $str2)) { $count++; } } return $count/count($str2)*100; } ?> PHP: Taken from http://us2.php.net/similar_text
Or you could just use the function itself, $i = similar_text($first_word, $second_word, &$p); echo("Matched: $i Percentage: $p%"); PHP: