I just wondering, whats happens here. If I am using this:
var_dump(similar_text('abcd', 'abcdefg', $percent)); //output: int 4
Thats ok, abcd
in the proper place, so 4 is good result.
Let's change a
and b
at the begining of the first variabl:
var_dump(similar_text('bacd', 'abcdefg', $percent)); //output: int 3
I excpected to 2
or 4
but not 3
. Can somebody explain it to me why is it?
similar_text() uses an algorithm that takes the first letter in the first string that the second string contains, counts that, and throws away the chars before that from the second string. This is the reason why we get different results.
Iteration for 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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With