function wordSimilarity($s1,$s2) {
$words1 = preg_split('/s+/',$s1);
$words2 = preg_split('/s+/',$s2);
$diffs1 = array_diff($words2,$words1);
$diffs2 = array_diff($words1,$words2);
$diffsLength = strlen(join("",$diffs1).join("",$diffs2));
$wordsLength = strlen(join("",$words1).join("",$words2));
if(!$wordsLength) return 0;
$differenceRate = ( $diffsLength / $wordsLength );
$similarityRate = 1 - $differenceRate;
return $similarityRate;
}
// This function gives you a floating point value between 0 and 1 where 1 is total similarity.
// Let's see some tests
$test = "this is something you've never done before";
wordSimilarity($test,"this is something you've never done before"); // 1.000
wordSimilarity($test,"this is something"); // 0.588
wordSimilarity($test,"this is nothing you have ever done"); // 0.312
wordSimilarity($test,"leave me alone with lorem ipsum"); // 0.000
wordSimilarity($test,"before you do something you've never done"); // 0.845
wordSimilarity($test,"never have i ever done this"); // 0.448