5

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?

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
vaso123
  • 12,347
  • 4
  • 34
  • 64
  • Its similarity in percentage, not number of characters it matched – Thamilhan May 31 '16 at 11:22
  • no, percentage will be in `$percent` variable, what I did not use here. – vaso123 May 31 '16 at 11:23
  • 2
    There's some discussion about it on this question: http://stackoverflow.com/questions/14136349/how-does-similar-text-work. Long story short, it's probably not doing what you think it is. – iainn May 31 '16 at 11:24
  • Not familiar with the algorithm used, but I'd suggest that `cd` in the correct location already, probably scoring a full point; and `ab` both contained only one character position differently probably scoring a half-point – Mark Baker May 31 '16 at 11:25
  • Watchout this: [how-to-improve-php-string-match-with-similar-text](http://stackoverflow.com/questions/10690854/how-to-improve-php-string-match-with-similar-text) – Murad Hasan May 31 '16 at 11:28
  • Can you explain why your expected output `2 or 4`?? – Murad Hasan May 31 '16 at 11:30
  • @FrayneKonok If function does not cares the order, then `a` and `b` could be found in the second string, so it should be `4`. If the function cares the order, then `a` and eithere `b` is not in the same place, in this case shoould be `2` – vaso123 May 31 '16 at 11:50

1 Answers1

3

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
Shijin TR
  • 7,516
  • 10
  • 55
  • 122