note 100344 deleted from function.similar-text by danbrown
| From: | danbrown@php.net | Date: | Sat, 09 Oct 2010 12:44:24 +0000 |
| Subject: | note 100344 deleted from function.similar-text by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173103@lists.php.net to get a copy of this message | ||
Note Submitter: Jirka Skuhrovec
----
Following functions is much more efficient for comparing short strings (say up to length 100). It
checks strings for containing same character PAIRS, rather than single characters. This better
captures differences in sensible text.
function getSimilarity($str1,$str2)
{
$arr1 = str_split($str1);
$arr2 = str_split($str2);
$i = 1;
$count = 0;
while(isset($arr1[$i]))
{
if(strstr($str2,$arr1[$i-1].$arr1[$i])) $count++;
$i++;
}
$i = 1;
while(isset($arr2[$i]))
{
if(strstr($str1,$arr2[$i-1].$arr2[$i])) $count++;
$i++;
}
return $count / (strlen($str1) + strlen($str2) -2);
}