3v4l.org

run code in 500+ 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, $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 = 12, Position 2 = 34
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 34
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 26
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
filename:       /in/ndM2U
function name:  (null)
number of ops:  36
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        ROPE_INIT                                         3  ~11     'Testing+%22'
          8        ROPE_ADD                                          1  ~11     ~11, !0
          9        ROPE_END                                          2  ~10     ~11, '%22%0A%0A'
         10        ECHO                                                         ~10
  116    11      > FE_RESET_R                                           $13     !1, ->34
         12    > > FE_FETCH_R                                                   $13, !3, ->34
  121    13    >   INIT_METHOD_CALL                                             !2, 'checkMatch'
         14        SEND_VAR_EX                                                  !3
         15        SEND_VAL_EX                                                  'levenshtein'
         16        SEND_VAL_EX                                                  <array>
         17        SEND_VAL_EX                                                  1
         18        DO_FCALL                                          0  $14     
         19        ASSIGN                                               ~15     !4, $14
         20      > JMPZ                                                         ~15, ->26
  122    21    >   ROPE_INIT                                         3  ~17     'matched+with+%22'
         22        ROPE_ADD                                          1  ~17     ~17, !4
         23        ROPE_END                                          2  ~16     ~17, '%22'
         24        QM_ASSIGN                                            ~19     ~16
         25      > JMP                                                          ->27
  123    26    >   QM_ASSIGN                                            ~19     'mismatched'
         27    >   ROPE_INIT                                         5  ~21     'Name+%22'
         28        ROPE_ADD                                          1  ~21     ~21, !3
         29        ROPE_ADD                                          2  ~21     ~21, '%22+has+'
         30        ROPE_ADD                                          3  ~21     ~21, ~19
         31        ROPE_END                                          4  ~20     ~21, '%0A'
         32        ECHO                                                         ~20
  116    33      > JMP                                                          ->12
         34    >   FE_FREE                                                      $13
  126    35      > RETURN                                                       1

Class Similarity:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ndM2U
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 = 30, Position 2 = 65
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 65
Branch analysis from position: 31
2 jumps found. (Code = 77) Position 1 = 35, Position 2 = 63
Branch analysis from position: 35
2 jumps found. (Code = 78) Position 1 = 36, Position 2 = 63
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 55, Position 2 = 62
Branch analysis from position: 55
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 58
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 58
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 63
Branch analysis from position: 65
2 jumps found. (Code = 43) Position 1 = 67, Position 2 = 69
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 65
filename:       /in/ndM2U
function name:  checkMatch
number of ops:  72
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        SEND_VAR_EX                                                  !0
          9        DO_FCALL                                          0  $10     
         10        ASSIGN                                                       !4, $10
   16    11        FETCH_DIM_R                                          ~12     !4, 'data'
         12        ASSIGN                                                       !5, ~12
   17    13        FETCH_DIM_R                                          ~14     !4, 'search'
         14        ASSIGN                                                       !0, ~14
   18    15        INIT_FCALL                                                   'preg_split'
         16        SEND_VAL                                                     '%2F%5Cs%2B%2F'
         17        SEND_VAR                                                     !5
         18        DO_ICALL                                             $16     
         19        ASSIGN                                                       !5, $16
   19    20        INIT_METHOD_CALL                                             'getAssoc'
         21        SEND_VAR_EX                                                  !5
         22        INIT_FCALL                                                   'substr_count'
         23        SEND_VAR                                                     !0
         24        SEND_VAL                                                     '+'
         25        DO_ICALL                                             $18     
         26        ADD                                                  ~19     $18, 1
         27        SEND_VAL_EX                                                  ~19
         28        DO_FCALL                                          0  $20     
         29      > FE_RESET_R                                           $21     $20, ->65
         30    > > FE_FETCH_R                                                   $21, !6, ->65
   21    31    >   INIT_METHOD_CALL                                             'getPermutations'
         32        SEND_VAR_EX                                                  !6
         33        DO_FCALL                                          0  $22     
         34      > FE_RESET_R                                           $23     $22, ->63
         35    > > FE_FETCH_R                                                   $23, !7, ->63
   23    36    >   INIT_FCALL                                                   'join'
         37        SEND_VAL                                                     '+'
         38        SEND_VAR                                                     !7
         39        DO_ICALL                                             $24     
         40        ASSIGN                                                       !7, $24
   24    41        INIT_USER_CALL                                    0          'call_user_func_array', !1
         42        INIT_FCALL                                                   'array_merge'
         43        INIT_ARRAY                                           ~26     !7
         44        ADD_ARRAY_ELEMENT                                    ~26     !0
         45        SEND_VAL                                                     ~26
         46        SEND_VAR                                                     !2
         47        DO_ICALL                                             $27     
         48        SEND_ARRAY                                                   $27
         49        CHECK_UNDEF_ARGS                                             
         50        DO_FCALL                                          1  $28     
         51        ASSIGN                                                       !8, $28
   25    52        FETCH_OBJ_R                                          ~30     'distance'
         53        IS_SMALLER_OR_EQUAL                                          !8, ~30
         54      > JMPZ                                                         ~31, ->62
   27    55    > > JMPZ                                                         !3, ->58
         56    >   QM_ASSIGN                                            ~32     !7
         57      > JMP                                                          ->59
         58    >   QM_ASSIGN                                            ~32     <true>
         59    >   FE_FREE                                                      $23
         60        FE_FREE                                                      $21
         61      > RETURN                                                       ~32
   21    62    > > JMP                                                          ->35
         63    >   FE_FREE                                                      $23
   19    64      > JMP                                                          ->30
         65    >   FE_FREE                                                      $21
   32    66      > JMPZ                                                         !3, ->69
         67    >   QM_ASSIGN                                            ~33     null
         68      > JMP                                                          ->70
         69    >   QM_ASSIGN                                            ~33     <false>
         70    > > RETURN                                                       ~33
   33    71*     > 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/ndM2U
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 = 39
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ndM2U
function name:  nextAssoc
number of ops:  41
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, ->39
   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        ADD                                                  ~8      !1, 2
         14        FRAMELESS_ICALL_3                substr              ~9      !0, 0
         15        OP_DATA                                                      ~8
   59    16        INIT_FCALL                                                   'str_repeat'
         17        SEND_VAL                                                     '0'
         18        INIT_FCALL                                                   'substr_count'
         19        ADD                                                  ~10     !1, 2
         20        FRAMELESS_ICALL_2                substr              ~11     !0, ~10
         21        SEND_VAL                                                     ~11
         22        SEND_VAL                                                     '0'
         23        DO_ICALL                                             $12     
         24        SEND_VAR                                                     $12
         25        DO_ICALL                                             $13     
         26        CONCAT                                               ~14     ~9, $13
   60    27        INIT_FCALL                                                   'str_repeat'
         28        SEND_VAL                                                     '1'
         29        INIT_FCALL                                                   'substr_count'
         30        ADD                                                  ~15     !1, 2
         31        FRAMELESS_ICALL_2                substr              ~16     !0, ~15
         32        SEND_VAL                                                     ~16
         33        SEND_VAL                                                     '1'
         34        DO_ICALL                                             $17     
         35        SEND_VAR                                                     $17
         36        DO_ICALL                                             $18     
         37        CONCAT                                               ~19     ~14, $18
         38      > RETURN                                                       ~19
   62    39    > > RETURN                                                       <false>
   63    40*     > 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/ndM2U
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 = 28, Position 2 = 40
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ndM2U
function name:  normalizeInput
number of ops:  54
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                                                   'strtolower'
          5        SEND_VAR                                                     !0
          6        DO_ICALL                                             $3      
          7        FRAMELESS_ICALL_1                trim                ~4      $3
          8        SEND_VAL                                                     ~4
          9        SEND_VAL                                                     -1
         10        SEND_VAL                                                     1
         11        DO_ICALL                                             $5      
         12        ASSIGN                                                       !0, $5
   84    13        INIT_FCALL                                                   'preg_split'
         14        SEND_VAL                                                     '%2F%5Cs%2B%2F'
         15        INIT_FCALL                                                   'strtolower'
         16        SEND_VAR                                                     !1
         17        DO_ICALL                                             $7      
         18        FRAMELESS_ICALL_1                trim                ~8      $7
         19        SEND_VAL                                                     ~8
         20        SEND_VAL                                                     -1
         21        SEND_VAL                                                     1
         22        DO_ICALL                                             $9      
         23        ASSIGN                                                       !1, $9
   85    24        COUNT                                                ~11     !0
         25        COUNT                                                ~12     !1
         26        IS_SMALLER                                                   ~11, ~12
         27      > JMPZ                                                         ~13, ->40
   87    28    >   ASSIGN                                                       !2, !0
   88    29        INIT_FCALL                                                   'join'
         30        SEND_VAL                                                     '+'
         31        SEND_VAR                                                     !1
         32        DO_ICALL                                             $15     
         33        ASSIGN                                                       !0, $15
   89    34        INIT_FCALL                                                   'join'
         35        SEND_VAL                                                     '+'
         36        SEND_VAR                                                     !2
         37        DO_ICALL                                             $17     
         38        ASSIGN                                                       !1, $17
   85    39      > JMP                                                          ->50
   93    40    >   INIT_FCALL                                                   'join'
         41        SEND_VAL                                                     '+'
         42        SEND_VAR                                                     !0
         43        DO_ICALL                                             $19     
         44        ASSIGN                                                       !0, $19
   94    45        INIT_FCALL                                                   'join'
         46        SEND_VAL                                                     '+'
         47        SEND_VAR                                                     !1
         48        DO_ICALL                                             $21     
         49        ASSIGN                                                       !1, $21
   96    50    >   INIT_ARRAY                                           ~23     !0, 'data'
         51        ADD_ARRAY_ELEMENT                                    ~23     !1, 'search'
         52      > RETURN                                                       ~23
   97    53*     > RETURN                                                       null

End of function normalizeinput

End of class Similarity.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
173.14 ms | 3505 KiB | 24 Q