3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Similarity { protected $data = null; protected $distance = null; public function __construct($data, $distance) { $this->data = (string)$data; $this->distance = (int)$distance; } public function checkMatch($search, callable $checker=null, array $args=[], $return=false) { $solid = $this->normalizeInput($this->data, $this->search); $data = $solid['data']; $search = $solid['search']; $data = preg_split('/\s+/', $data); foreach($this->getAssoc($data, substr_count($search, ' ')+1) as $assoc) { foreach($this->getPermutations($assoc) as $ordered) { $ordered = join(' ', $ordered); $result = call_user_func_array($checker, array_merge([$ordered, $search], $args)); if($result<=$this->distance) { return $return?$ordered:true; } } } return $return?null:false; } protected function getPermutations(array $input) { if(count($input)==1) { return [$input]; } $result = []; foreach($input as $key=>$element) { foreach($this->getPermutations(array_diff_key($input, [$key=>0])) as $subarray) { $result[] = array_merge([$element], $subarray); } } return $result; } protected function nextAssoc($assoc) { if(false !== ($pos = strrpos($assoc, '01'))) { $assoc[$pos] = '1'; $assoc[$pos+1] = '0'; return substr($assoc, 0, $pos+2). str_repeat('0', substr_count(substr($assoc, $pos+2), '0')). str_repeat('1', substr_count(substr($assoc, $pos+2), '1')); } return false; } protected function getAssoc(array $data, $count=2) { if(count($data)<$count) { return null; } $assoc = str_repeat('0', count($data)-$count).str_repeat('1', $count); $result = []; do { $result[]=array_intersect_key($data, array_filter(str_split($assoc))); } while($assoc=$this->nextAssoc($assoc)); return $result; } protected function normalizeInput($data, $search) { $data = preg_split('/\s+/', trim(strtolower($data)), -1, PREG_SPLIT_NO_EMPTY); $search = preg_split('/\s+/', trim(strtolower($search)), -1, PREG_SPLIT_NO_EMPTY); if(count($data)<count($search)) { $temp = $data; $data = join(' ', $search); $search = join(' ', $temp); } else { $data = join(' ', $data); $search = join(' ', $search); } return ['data'=>$data, 'search'=>$search]; } } $data = 'Louise Bro'; $search = [ 'Louise Gulbæk Bro', 'Niels Faurskov', 'Niels Faurskov Andersen', 'Nils Faurskov Andersen', 'Nils Andersen', 'niels faurskov', 'niels Fæurskov', 'niffddels Faurskævffre', 'jens grunnet' ]; $checker = new Similarity($data, 2); echo(sprintf('Testing "%s"'.PHP_EOL.PHP_EOL, $data)); foreach($search as $name) { echo(sprintf( 'Name "%s" has %s'.PHP_EOL, $name, ($result=$checker->checkMatch($name, 'levenshtein', [], 1)) ?sprintf('matched with "%s"', $result) :'mismatched' ) ); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 36
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 36
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 31
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
filename:       /in/vuLAk
function name:  (null)
number of ops:  38
compiled vars:  !0 = $data, !1 = $search, !2 = $checker, !3 = $name, !4 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  100     0  E >   ASSIGN                                                   !0, 'Louise+Bro'
  101     1        ASSIGN                                                   !1, <array>
  113     2        NEW                                              $7      'Similarity'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAL_EX                                              2
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !2, $7
  115     7        INIT_FCALL                                               'sprintf'
          8        SEND_VAL                                                 'Testing+%22%25s%22%0A%0A'
          9        SEND_VAR                                                 !0
         10        DO_ICALL                                         $10     
         11        ECHO                                                     $10
  116    12      > FE_RESET_R                                       $11     !1, ->36
         13    > > FE_FETCH_R                                               $11, !3, ->36
  118    14    >   INIT_FCALL                                               'sprintf'
  119    15        SEND_VAL                                                 'Name+%22%25s%22+has+%25s%0A'
  120    16        SEND_VAR                                                 !3
  121    17        INIT_METHOD_CALL                                         !2, 'checkMatch'
         18        SEND_VAR_EX                                              !3
         19        SEND_VAL_EX                                              'levenshtein'
         20        SEND_VAL_EX                                              <array>
         21        SEND_VAL_EX                                              1
         22        DO_FCALL                                      0  $12     
         23        ASSIGN                                           ~13     !4, $12
         24      > JMPZ                                                     ~13, ->31
  122    25    >   INIT_FCALL                                               'sprintf'
         26        SEND_VAL                                                 'matched+with+%22%25s%22'
         27        SEND_VAR                                                 !4
         28        DO_ICALL                                         $14     
         29        QM_ASSIGN                                        ~15     $14
         30      > JMP                                                      ->32
  123    31    >   QM_ASSIGN                                        ~15     'mismatched'
         32    >   SEND_VAL                                                 ~15
         33        DO_ICALL                                         $16     
         34        ECHO                                                     $16
  116    35      > JMP                                                      ->13
         36    >   FE_FREE                                                  $11
  126    37      > RETURN                                                   1

Class Similarity:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vuLAk
function name:  __construct
number of ops:  9
compiled vars:  !0 = $data, !1 = $distance
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    9     2        CAST                                          6  ~3      !0
          3        ASSIGN_OBJ                                               'data'
          4        OP_DATA                                                  ~3
   10     5        CAST                                          4  ~5      !1
          6        ASSIGN_OBJ                                               'distance'
          7        OP_DATA                                                  ~5
   11     8      > RETURN                                                   null

End of function __construct

Function checkmatch:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 67
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 67
Branch analysis from position: 33
2 jumps found. (Code = 77) Position 1 = 37, Position 2 = 65
Branch analysis from position: 37
2 jumps found. (Code = 78) Position 1 = 38, Position 2 = 65
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 64
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 60
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 65
Branch analysis from position: 67
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 71
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 72
Branch analysis from position: 72
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 71
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 67
filename:       /in/vuLAk
function name:  checkMatch
number of ops:  74
compiled vars:  !0 = $search, !1 = $checker, !2 = $args, !3 = $return, !4 = $solid, !5 = $data, !6 = $assoc, !7 = $ordered, !8 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
          2        RECV_INIT                                        !2      <array>
          3        RECV_INIT                                        !3      <false>
   15     4        INIT_METHOD_CALL                                         'normalizeInput'
          5        CHECK_FUNC_ARG                                           
          6        FETCH_OBJ_FUNC_ARG                               $9      'data'
          7        SEND_FUNC_ARG                                            $9
          8        CHECK_FUNC_ARG                                           
          9        FETCH_OBJ_FUNC_ARG                               $10     'search'
         10        SEND_FUNC_ARG                                            $10
         11        DO_FCALL                                      0  $11     
         12        ASSIGN                                                   !4, $11
   16    13        FETCH_DIM_R                                      ~13     !4, 'data'
         14        ASSIGN                                                   !5, ~13
   17    15        FETCH_DIM_R                                      ~15     !4, 'search'
         16        ASSIGN                                                   !0, ~15
   18    17        INIT_FCALL                                               'preg_split'
         18        SEND_VAL                                                 '%2F%5Cs%2B%2F'
         19        SEND_VAR                                                 !5
         20        DO_ICALL                                         $17     
         21        ASSIGN                                                   !5, $17
   19    22        INIT_METHOD_CALL                                         'getAssoc'
         23        SEND_VAR_EX                                              !5
         24        INIT_FCALL                                               'substr_count'
         25        SEND_VAR                                                 !0
         26        SEND_VAL                                                 '+'
         27        DO_ICALL                                         $19     
         28        ADD                                              ~20     $19, 1
         29        SEND_VAL_EX                                              ~20
         30        DO_FCALL                                      0  $21     
         31      > FE_RESET_R                                       $22     $21, ->67
         32    > > FE_FETCH_R                                               $22, !6, ->67
   21    33    >   INIT_METHOD_CALL                                         'getPermutations'
         34        SEND_VAR_EX                                              !6
         35        DO_FCALL                                      0  $23     
         36      > FE_RESET_R                                       $24     $23, ->65
         37    > > FE_FETCH_R                                               $24, !7, ->65
   23    38    >   INIT_FCALL                                               'join'
         39        SEND_VAL                                                 '+'
         40        SEND_VAR                                                 !7
         41        DO_ICALL                                         $25     
         42        ASSIGN                                                   !7, $25
   24    43        INIT_USER_CALL                                0          'call_user_func_array', !1
         44        INIT_FCALL                                               'array_merge'
         45        INIT_ARRAY                                       ~27     !7
         46        ADD_ARRAY_ELEMENT                                ~27     !0
         47        SEND_VAL                                                 ~27
         48        SEND_VAR                                                 !2
         49        DO_ICALL                                         $28     
         50        SEND_ARRAY                                               $28
         51        CHECK_UNDEF_ARGS                                         
         52        DO_FCALL                                      0  $29     
         53        ASSIGN                                                   !8, $29
   25    54        FETCH_OBJ_R                                      ~31     'distance'
         55        IS_SMALLER_OR_EQUAL                                      !8, ~31
         56      > JMPZ                                                     ~32, ->64
   27    57    > > JMPZ                                                     !3, ->60
         58    >   QM_ASSIGN                                        ~33     !7
         59      > JMP                                                      ->61
         60    >   QM_ASSIGN                                        ~33     <true>
         61    >   FE_FREE                                                  $24
         62        FE_FREE                                                  $22
         63      > RETURN                                                   ~33
   21    64    > > JMP                                                      ->37
         65    >   FE_FREE                                                  $24
   19    66      > JMP                                                      ->32
         67    >   FE_FREE                                                  $22
   32    68      > JMPZ                                                     !3, ->71
         69    >   QM_ASSIGN                                        ~34     null
         70      > JMP                                                      ->72
         71    >   QM_ASSIGN                                        ~34     <false>
         72    > > RETURN                                                   ~34
   33    73*     > RETURN                                                   null

End of function checkmatch

Function getpermutations:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 30
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 30
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 28
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 28
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 28
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/vuLAk
function name:  getPermutations
number of ops:  33
compiled vars:  !0 = $input, !1 = $result, !2 = $element, !3 = $key, !4 = $subarray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
   37     1        COUNT                                            ~5      !0
          2        IS_EQUAL                                                 ~5, 1
          3      > JMPZ                                                     ~6, ->6
   39     4    >   INIT_ARRAY                                       ~7      !0
          5      > RETURN                                                   ~7
   41     6    >   ASSIGN                                                   !1, <array>
   42     7      > FE_RESET_R                                       $9      !0, ->30
          8    > > FE_FETCH_R                                       ~10     $9, !2, ->30
          9    >   ASSIGN                                                   !3, ~10
   44    10        INIT_METHOD_CALL                                         'getPermutations'
         11        INIT_FCALL                                               'array_diff_key'
         12        SEND_VAR                                                 !0
         13        INIT_ARRAY                                       ~12     0, !3
         14        SEND_VAL                                                 ~12
         15        DO_ICALL                                         $13     
         16        SEND_VAR_NO_REF_EX                                       $13
         17        DO_FCALL                                      0  $14     
         18      > FE_RESET_R                                       $15     $14, ->28
         19    > > FE_FETCH_R                                               $15, !4, ->28
   46    20    >   INIT_FCALL                                               'array_merge'
         21        INIT_ARRAY                                       ~17     !2
         22        SEND_VAL                                                 ~17
         23        SEND_VAR                                                 !4
         24        DO_ICALL                                         $18     
         25        ASSIGN_DIM                                               !1
         26        OP_DATA                                                  $18
   44    27      > JMP                                                      ->19
         28    >   FE_FREE                                                  $15
   42    29      > JMP                                                      ->8
         30    >   FE_FREE                                                  $9
   49    31      > RETURN                                                   !1
   50    32*     > RETURN                                                   null

End of function getpermutations

Function nextassoc:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 48
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vuLAk
function name:  nextAssoc
number of ops:  50
compiled vars:  !0 = $assoc, !1 = $pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   54     1        INIT_FCALL                                               'strrpos'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 '01'
          4        DO_ICALL                                         $2      
          5        ASSIGN                                           ~3      !1, $2
          6        TYPE_CHECK                                  1018          ~3
          7      > JMPZ                                                     ~4, ->48
   56     8    >   ASSIGN_DIM                                               !0, !1
          9        OP_DATA                                                  '1'
   57    10        ADD                                              ~6      !1, 1
         11        ASSIGN_DIM                                               !0, ~6
         12        OP_DATA                                                  '0'
   58    13        INIT_FCALL                                               'substr'
         14        SEND_VAR                                                 !0
         15        SEND_VAL                                                 0
         16        ADD                                              ~8      !1, 2
         17        SEND_VAL                                                 ~8
         18        DO_ICALL                                         $9      
   59    19        INIT_FCALL                                               'str_repeat'
         20        SEND_VAL                                                 '0'
         21        INIT_FCALL                                               'substr_count'
         22        INIT_FCALL                                               'substr'
         23        SEND_VAR                                                 !0
         24        ADD                                              ~10     !1, 2
         25        SEND_VAL                                                 ~10
         26        DO_ICALL                                         $11     
         27        SEND_VAR                                                 $11
         28        SEND_VAL                                                 '0'
         29        DO_ICALL                                         $12     
         30        SEND_VAR                                                 $12
         31        DO_ICALL                                         $13     
         32        CONCAT                                           ~14     $9, $13
   60    33        INIT_FCALL                                               'str_repeat'
         34        SEND_VAL                                                 '1'
         35        INIT_FCALL                                               'substr_count'
         36        INIT_FCALL                                               'substr'
         37        SEND_VAR                                                 !0
         38        ADD                                              ~15     !1, 2
         39        SEND_VAL                                                 ~15
         40        DO_ICALL                                         $16     
         41        SEND_VAR                                                 $16
         42        SEND_VAL                                                 '1'
         43        DO_ICALL                                         $17     
         44        SEND_VAR                                                 $17
         45        DO_ICALL                                         $18     
         46        CONCAT                                           ~19     ~14, $18
         47      > RETURN                                                   ~19
   62    48    > > RETURN                                                   <false>
   63    49*     > RETURN                                                   null

End of function nextassoc

Function getassoc:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 19
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/vuLAk
function name:  getAssoc
number of ops:  38
compiled vars:  !0 = $data, !1 = $count, !2 = $assoc, !3 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      2
   67     2        COUNT                                            ~4      !0
          3        IS_SMALLER                                               ~4, !1
          4      > JMPZ                                                     ~5, ->6
   69     5    > > RETURN                                                   null
   71     6    >   INIT_FCALL                                               'str_repeat'
          7        SEND_VAL                                                 '0'
          8        COUNT                                            ~6      !0
          9        SUB                                              ~7      ~6, !1
         10        SEND_VAL                                                 ~7
         11        DO_ICALL                                         $8      
         12        INIT_FCALL                                               'str_repeat'
         13        SEND_VAL                                                 '1'
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                         $9      
         16        CONCAT                                           ~10     $8, $9
         17        ASSIGN                                                   !2, ~10
   72    18        ASSIGN                                                   !3, <array>
   75    19    >   INIT_FCALL                                               'array_intersect_key'
         20        SEND_VAR                                                 !0
         21        INIT_FCALL                                               'array_filter'
         22        INIT_FCALL                                               'str_split'
         23        SEND_VAR                                                 !2
         24        DO_ICALL                                         $14     
         25        SEND_VAR                                                 $14
         26        DO_ICALL                                         $15     
         27        SEND_VAR                                                 $15
         28        DO_ICALL                                         $16     
         29        ASSIGN_DIM                                               !3
         30        OP_DATA                                                  $16
   77    31        INIT_METHOD_CALL                                         'nextAssoc'
         32        SEND_VAR_EX                                              !2
         33        DO_FCALL                                      0  $17     
         34        ASSIGN                                           ~18     !2, $17
         35      > JMPNZ                                                    ~18, ->19
   78    36    > > RETURN                                                   !3
   79    37*     > RETURN                                                   null

End of function getassoc

Function normalizeinput:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 44
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vuLAk
function name:  normalizeInput
number of ops:  58
compiled vars:  !0 = $data, !1 = $search, !2 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   83     2        INIT_FCALL                                               'preg_split'
          3        SEND_VAL                                                 '%2F%5Cs%2B%2F'
          4        INIT_FCALL                                               'trim'
          5        INIT_FCALL                                               'strtolower'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $3      
          8        SEND_VAR                                                 $3
          9        DO_ICALL                                         $4      
         10        SEND_VAR                                                 $4
         11        SEND_VAL                                                 -1
         12        SEND_VAL                                                 1
         13        DO_ICALL                                         $5      
         14        ASSIGN                                                   !0, $5
   84    15        INIT_FCALL                                               'preg_split'
         16        SEND_VAL                                                 '%2F%5Cs%2B%2F'
         17        INIT_FCALL                                               'trim'
         18        INIT_FCALL                                               'strtolower'
         19        SEND_VAR                                                 !1
         20        DO_ICALL                                         $7      
         21        SEND_VAR                                                 $7
         22        DO_ICALL                                         $8      
         23        SEND_VAR                                                 $8
         24        SEND_VAL                                                 -1
         25        SEND_VAL                                                 1
         26        DO_ICALL                                         $9      
         27        ASSIGN                                                   !1, $9
   85    28        COUNT                                            ~11     !0
         29        COUNT                                            ~12     !1
         30        IS_SMALLER                                               ~11, ~12
         31      > JMPZ                                                     ~13, ->44
   87    32    >   ASSIGN                                                   !2, !0
   88    33        INIT_FCALL                                               'join'
         34        SEND_VAL                                                 '+'
         35        SEND_VAR                                                 !1
         36        DO_ICALL                                         $15     
         37        ASSIGN                                                   !0, $15
   89    38        INIT_FCALL                                               'join'
         39        SEND_VAL                                                 '+'
         40        SEND_VAR                                                 !2
         41        DO_ICALL                                         $17     
         42        ASSIGN                                                   !1, $17
         43      > JMP                                                      ->54
   93    44    >   INIT_FCALL                                               'join'
         45        SEND_VAL                                                 '+'
         46        SEND_VAR                                                 !0
         47        DO_ICALL                                         $19     
         48        ASSIGN                                                   !0, $19
   94    49        INIT_FCALL                                               'join'
         50        SEND_VAL                                                 '+'
         51        SEND_VAR                                                 !1
         52        DO_ICALL                                         $21     
         53        ASSIGN                                                   !1, $21
   96    54    >   INIT_ARRAY                                       ~23     !0, 'data'
         55        ADD_ARRAY_ELEMENT                                ~23     !1, 'search'
         56      > RETURN                                                   ~23
   97    57*     > RETURN                                                   null

End of function normalizeinput

End of class Similarity.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
165.5 ms | 1424 KiB | 41 Q