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) { $data = preg_split('/\s+/', strtolower($this->data), -1, PREG_SPLIT_NO_EMPTY); $search = trim(preg_replace('/\s+/', ' ', strtolower($search))); 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; } } $data = 'Niels Faurskov Andersen'; $search = [ 'Niels Andersen', 'Niels Faurskov', 'Niels Faurskov Andersen', 'Nils Faurskov Andersen', 'Nils Andersen', 'niels faurskov', 'niels Faurskov', 'niffddels Faurskovffre' ]; $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/AvH6H
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
-------------------------------------------------------------------------------------
   80     0  E >   ASSIGN                                                   !0, 'Niels+Faurskov+Andersen'
   81     1        ASSIGN                                                   !1, <array>
   92     2        NEW                                              $7      'Similarity'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAL_EX                                              2
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !2, $7
   94     7        INIT_FCALL                                               'sprintf'
          8        SEND_VAL                                                 'Testing+%22%25s%22%0A%0A'
          9        SEND_VAR                                                 !0
         10        DO_ICALL                                         $10     
         11        ECHO                                                     $10
   95    12      > FE_RESET_R                                       $11     !1, ->36
         13    > > FE_FETCH_R                                               $11, !3, ->36
   97    14    >   INIT_FCALL                                               'sprintf'
   98    15        SEND_VAL                                                 'Name+%22%25s%22+has+%25s%0A'
   99    16        SEND_VAR                                                 !3
  100    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
  101    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
  102    31    >   QM_ASSIGN                                        ~15     'mismatched'
         32    >   SEND_VAL                                                 ~15
         33        DO_ICALL                                         $16     
         34        ECHO                                                     $16
   95    35      > JMP                                                      ->13
         36    >   FE_FREE                                                  $11
  105    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/AvH6H
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 = 37, Position 2 = 72
Branch analysis from position: 37
2 jumps found. (Code = 78) Position 1 = 38, Position 2 = 72
Branch analysis from position: 38
2 jumps found. (Code = 77) Position 1 = 42, Position 2 = 70
Branch analysis from position: 42
2 jumps found. (Code = 78) Position 1 = 43, Position 2 = 70
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 69
Branch analysis from position: 62
2 jumps found. (Code = 43) Position 1 = 63, Position 2 = 65
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 66
Branch analysis from position: 66
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
Branch analysis from position: 70
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 70
Branch analysis from position: 72
2 jumps found. (Code = 43) Position 1 = 74, Position 2 = 76
Branch analysis from position: 74
1 jumps found. (Code = 42) Position 1 = 77
Branch analysis from position: 77
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 76
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 72
filename:       /in/AvH6H
function name:  checkMatch
number of ops:  79
compiled vars:  !0 = $search, !1 = $checker, !2 = $args, !3 = $return, !4 = $data, !5 = $assoc, !6 = $ordered, !7 = $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_FCALL                                               'preg_split'
          5        SEND_VAL                                                 '%2F%5Cs%2B%2F'
          6        INIT_FCALL                                               'strtolower'
          7        FETCH_OBJ_R                                      ~8      'data'
          8        SEND_VAL                                                 ~8
          9        DO_ICALL                                         $9      
         10        SEND_VAR                                                 $9
         11        SEND_VAL                                                 -1
         12        SEND_VAL                                                 1
         13        DO_ICALL                                         $10     
         14        ASSIGN                                                   !4, $10
   16    15        INIT_FCALL                                               'trim'
         16        INIT_FCALL                                               'preg_replace'
         17        SEND_VAL                                                 '%2F%5Cs%2B%2F'
         18        SEND_VAL                                                 '+'
         19        INIT_FCALL                                               'strtolower'
         20        SEND_VAR                                                 !0
         21        DO_ICALL                                         $12     
         22        SEND_VAR                                                 $12
         23        DO_ICALL                                         $13     
         24        SEND_VAR                                                 $13
         25        DO_ICALL                                         $14     
         26        ASSIGN                                                   !0, $14
   17    27        INIT_METHOD_CALL                                         'getAssoc'
         28        SEND_VAR_EX                                              !4
         29        INIT_FCALL                                               'substr_count'
         30        SEND_VAR                                                 !0
         31        SEND_VAL                                                 '+'
         32        DO_ICALL                                         $16     
         33        ADD                                              ~17     $16, 1
         34        SEND_VAL_EX                                              ~17
         35        DO_FCALL                                      0  $18     
         36      > FE_RESET_R                                       $19     $18, ->72
         37    > > FE_FETCH_R                                               $19, !5, ->72
   19    38    >   INIT_METHOD_CALL                                         'getPermutations'
         39        SEND_VAR_EX                                              !5
         40        DO_FCALL                                      0  $20     
         41      > FE_RESET_R                                       $21     $20, ->70
         42    > > FE_FETCH_R                                               $21, !6, ->70
   21    43    >   INIT_FCALL                                               'join'
         44        SEND_VAL                                                 '+'
         45        SEND_VAR                                                 !6
         46        DO_ICALL                                         $22     
         47        ASSIGN                                                   !6, $22
   22    48        INIT_USER_CALL                                0          'call_user_func_array', !1
         49        INIT_FCALL                                               'array_merge'
         50        INIT_ARRAY                                       ~24     !6
         51        ADD_ARRAY_ELEMENT                                ~24     !0
         52        SEND_VAL                                                 ~24
         53        SEND_VAR                                                 !2
         54        DO_ICALL                                         $25     
         55        SEND_ARRAY                                               $25
         56        CHECK_UNDEF_ARGS                                         
         57        DO_FCALL                                      0  $26     
         58        ASSIGN                                                   !7, $26
   23    59        FETCH_OBJ_R                                      ~28     'distance'
         60        IS_SMALLER_OR_EQUAL                                      !7, ~28
         61      > JMPZ                                                     ~29, ->69
   25    62    > > JMPZ                                                     !3, ->65
         63    >   QM_ASSIGN                                        ~30     !6
         64      > JMP                                                      ->66
         65    >   QM_ASSIGN                                        ~30     <true>
         66    >   FE_FREE                                                  $21
         67        FE_FREE                                                  $19
         68      > RETURN                                                   ~30
   19    69    > > JMP                                                      ->42
         70    >   FE_FREE                                                  $21
   17    71      > JMP                                                      ->37
         72    >   FE_FREE                                                  $19
   30    73      > JMPZ                                                     !3, ->76
         74    >   QM_ASSIGN                                        ~31     null
         75      > JMP                                                      ->77
         76    >   QM_ASSIGN                                        ~31     <false>
         77    > > RETURN                                                   ~31
   31    78*     > 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/AvH6H
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
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   35     1        COUNT                                            ~5      !0
          2        IS_EQUAL                                                 ~5, 1
          3      > JMPZ                                                     ~6, ->6
   37     4    >   INIT_ARRAY                                       ~7      !0
          5      > RETURN                                                   ~7
   39     6    >   ASSIGN                                                   !1, <array>
   40     7      > FE_RESET_R                                       $9      !0, ->30
          8    > > FE_FETCH_R                                       ~10     $9, !2, ->30
          9    >   ASSIGN                                                   !3, ~10
   42    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
   44    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
   42    27      > JMP                                                      ->19
         28    >   FE_FREE                                                  $15
   40    29      > JMP                                                      ->8
         30    >   FE_FREE                                                  $9
   47    31      > RETURN                                                   !1
   48    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/AvH6H
function name:  nextAssoc
number of ops:  50
compiled vars:  !0 = $assoc, !1 = $pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
   52     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
   54     8    >   ASSIGN_DIM                                               !0, !1
          9        OP_DATA                                                  '1'
   55    10        ADD                                              ~6      !1, 1
         11        ASSIGN_DIM                                               !0, ~6
         12        OP_DATA                                                  '0'
   56    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      
   57    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
   58    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
   60    48    > > RETURN                                                   <false>
   61    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/AvH6H
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
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      2
   65     2        COUNT                                            ~4      !0
          3        IS_SMALLER                                               ~4, !1
          4      > JMPZ                                                     ~5, ->6
   67     5    > > RETURN                                                   null
   69     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
   70    18        ASSIGN                                                   !3, <array>
   73    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
   75    31        INIT_METHOD_CALL                                         'nextAssoc'
         32        SEND_VAR_EX                                              !2
         33        DO_FCALL                                      0  $17     
         34        ASSIGN                                           ~18     !2, $17
         35      > JMPNZ                                                    ~18, ->19
   76    36    > > RETURN                                                   !3
   77    37*     > RETURN                                                   null

End of function getassoc

End of class Similarity.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.6 ms | 1412 KiB | 43 Q