1

I am using similar_text() function to Calculate the similarity between two strings.

$s1 = 'God is great'; 
$s2 = 'I too'; 

similar_text($s1, $s2, $result); 

echo $result; 

It gives output 11.764705882353 but when i interchange the position of strings, it gives different output:

$s1 = 'God is great'; 
$s2 = 'I too'; 

similar_text($s2, $s1, $result); 

echo $result; 

It gives output 23.529411764706 Why is this happen?

Animay
  • 602
  • 1
  • 7
  • 18

3 Answers3

1

The function uses different logic depending of the parameter order. Check this question How does similar_text work?

This bug was reported without answer too https://bugs.php.net/bug.php?id=62648

Community
  • 1
  • 1
0

Algorithm takes the first letter in the first string that second string contains, counts that, and throws away the chars before that from the second string. That is why it misses the characters in-between, and that's the thing causing the difference when you change the character order.

eisbach
  • 417
  • 3
  • 7
0

I'm not entirely sure why similar_text() produces different results, In the meanwhile, you can use levenshtein(), which will produce coherent results and do what you need, i.e.:

echo levenshtein ($s2, $s1);
# 11
echo levenshtein ($s1, $s2);
# 11
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268