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) { $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 = 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/AvH6H
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
-----------------------------------------------------------------------------------------
   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        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
   95    11      > FE_RESET_R                                           $13     !1, ->34
         12    > > FE_FETCH_R                                                   $13, !3, ->34
  100    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
  101    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
  102    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
   95    33      > JMP                                                          ->12
         34    >   FE_FREE                                                      $13
  105    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/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 = 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/AvH6H
function name:  checkMatch
number of ops:  74
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                                                   'strtolower'
         16        SEND_VAR                                                     !0
         17        DO_ICALL                                             $12     
         18        FRAMELESS_ICALL_3                preg_replace        ~13     '%2F%5Cs%2B%2F', '+'
         19        OP_DATA                                                      $12
         20        FRAMELESS_ICALL_1                trim                ~14     ~13
         21        ASSIGN                                                       !0, ~14
   17    22        INIT_METHOD_CALL                                             'getAssoc'
         23        SEND_VAR_EX                                                  !4
         24        INIT_FCALL                                                   'substr_count'
         25        SEND_VAR                                                     !0
         26        SEND_VAL                                                     '+'
         27        DO_ICALL                                             $16     
         28        ADD                                                  ~17     $16, 1
         29        SEND_VAL_EX                                                  ~17
         30        DO_FCALL                                          0  $18     
         31      > FE_RESET_R                                           $19     $18, ->67
         32    > > FE_FETCH_R                                                   $19, !5, ->67
   19    33    >   INIT_METHOD_CALL                                             'getPermutations'
         34        SEND_VAR_EX                                                  !5
         35        DO_FCALL                                          0  $20     
         36      > FE_RESET_R                                           $21     $20, ->65
         37    > > FE_FETCH_R                                                   $21, !6, ->65
   21    38    >   INIT_FCALL                                                   'join'
         39        SEND_VAL                                                     '+'
         40        SEND_VAR                                                     !6
         41        DO_ICALL                                             $22     
         42        ASSIGN                                                       !6, $22
   22    43        INIT_USER_CALL                                    0          'call_user_func_array', !1
         44        INIT_FCALL                                                   'array_merge'
         45        INIT_ARRAY                                           ~24     !6
         46        ADD_ARRAY_ELEMENT                                    ~24     !0
         47        SEND_VAL                                                     ~24
         48        SEND_VAR                                                     !2
         49        DO_ICALL                                             $25     
         50        SEND_ARRAY                                                   $25
         51        CHECK_UNDEF_ARGS                                             
         52        DO_FCALL                                          1  $26     
         53        ASSIGN                                                       !7, $26
   23    54        FETCH_OBJ_R                                          ~28     'distance'
         55        IS_SMALLER_OR_EQUAL                                          !7, ~28
         56      > JMPZ                                                         ~29, ->64
   25    57    > > JMPZ                                                         !3, ->60
         58    >   QM_ASSIGN                                            ~30     !6
         59      > JMP                                                          ->61
         60    >   QM_ASSIGN                                            ~30     <true>
         61    >   FE_FREE                                                      $21
         62        FE_FREE                                                      $19
         63      > RETURN                                                       ~30
   19    64    > > JMP                                                          ->37
         65    >   FE_FREE                                                      $21
   17    66      > JMP                                                          ->32
         67    >   FE_FREE                                                      $19
   30    68      > JMPZ                                                         !3, ->71
         69    >   QM_ASSIGN                                            ~31     null
         70      > JMP                                                          ->72
         71    >   QM_ASSIGN                                            ~31     <false>
         72    > > RETURN                                                       ~31
   31    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/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 = 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/AvH6H
function name:  nextAssoc
number of ops:  41
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, ->39
   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        ADD                                                  ~8      !1, 2
         14        FRAMELESS_ICALL_3                substr              ~9      !0, 0
         15        OP_DATA                                                      ~8
   57    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
   58    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
   60    39    > > RETURN                                                       <false>
   61    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/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.5.0


preferences:
223.27 ms | 3542 KiB | 24 Q