Similar_text does not produce the expected result

I'm just wondering what's going on here. If I use this:

var_dump(similar_text('abcd', 'abcdefg', $percent)); //output: int 4 

Ok, abcd in the right place, so 4 is a good result.

At the beginning of the first option, change a and b :

 var_dump(similar_text('bacd', 'abcdefg', $percent)); //output: int 3 

I tried to 2 or 4 , but not 3 . Can someone explain this to me why?

+5
source share
1 answer

Similar_text () uses an algorithm that takes the first letter in the first line containing the second line, considers this and discards the characters before it from the second line. That is why we get different results.

Iteration for the first example

  'abcd' vs 'abcdefg' - (1) // 'a' match with 'a' 'bcd' vs 'bcdefg' - (1) // 'b' match with 'b' 'cd' vs 'cdefg' - (1) // 'c' match with 'c' 'd' vs 'defg' - (1) // 'd' match with 'd' '' vs 'efg' - (0) // no match Result = 4 

Iteration for the second example

  'bacd' vs 'abcdefg' - (0) // b not match a 'bacd' vs 'bcdefg' - (1) // b match b 'acd' vs 'cdefg' - (0) // a not match c 'cd' vs 'cdefg' - (1) // c match c 'd' vs 'defg' - (1) // d match d '' vs 'efg' - (0) // not match with any elemennt Result = 3 
+3
source

All Articles