Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

similar_text not giving expected result

Tags:

php

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?

like image 894
vaso123 Avatar asked Oct 19 '22 08:10

vaso123


1 Answers

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
like image 192
Shijin TR Avatar answered Oct 21 '22 04:10

Shijin TR