3v4l.org

run code in 300+ PHP versions simultaneously
<?php function find_similar3($needle,$str,$keep_needle_order=false){ if(!is_string($needle)||!is_string($str)) { return false; } $valid=array(); //get encodings and words from haystack and needle setlocale(LC_CTYPE, 'en_GB.UTF8'); $encoding_s=mb_detect_encoding($str); $encoding_n=mb_detect_encoding($needle); mb_regex_encoding ($encoding_n); $pneed=array_filter(mb_split('\W',$needle)); mb_regex_encoding ($encoding_s); $pstr=array_filter(mb_split('\W',$str)); foreach($pneed as $k=>$word)//loop trough needle's words { foreach($pstr as $key=>$w) { if($encoding_n!==$encoding_s) {//if $encodings are not the same make some transliteration // $tmp_word=($encoding_n!=='ASCII')?to_ascii($word,$encoding_n):$word; // $tmp_w=($encoding_s!=='ASCII')?to_ascii($w,$encoding_s):$w; $tmp_word=($encoding_n!=='ASCII')?mb_convert_encoding($word,$encoding_n, 'ASCII'):$word; $tmp_w=($encoding_s!=='ASCII')?mb_convert_encoding($w,$encoding_s, 'ASCII'):$w; }else { $tmp_word=$word; $tmp_w=$w; } $tmp[$tmp_w]=levenshtein($tmp_w,$tmp_word);//collect levenshtein distances $keys[$tmp_w]=array($key,$w); } $nominees=array_flip(array_keys($tmp,min($tmp)));//get the nominees $tmp=10000; foreach($nominees as $nominee=>$idx) {//test sound like to get more precision $idx=levenshtein(metaphone($nominee),metaphone($tmp_word)); if($idx<$tmp){ $answer=$nominee;//get the winner } unset($nominees[$nominee]); } if(!$keep_needle_order){ $valid[$keys[$answer][0]]=$keys[$answer][1];//get the right form of the winner } else{ $valid[$k]=$keys[$answer][1]; } $tmp=$nominees=array();//clean a little for the next iteration } if(!$keep_needle_order) { ksort($valid); } $valid=array_values($valid);//get only the values /*return the array of the closest value to the needle according to this algorithm of course*/ return $valid; } var_dump(find_similar3('i knew you love me','finally i know you loved me and all my pets')); var_dump(find_similar3('I you love','This is a demo text and I love you about this')); var_dump(find_similar3('a unik idia','I have a unique idea. Do you need?')); var_dump(find_similar3("Goebel, Weiss, Goethe, Goethe und Goetz",'Weiß, Goldmann, Göbel, Weiss, Göthe, Goethe und Götz')); var_dump(find_similar3('Ḽơᶉëᶆ ȋṕšᶙṁ ḍỡḽǭᵳ ʂǐť ӓṁệẗ, ĉṓɲṩḙċťᶒțûɾ ấɖḯƥĭṩčįɳġ ḝłįʈ', 'Ḽơᶉëᶆ ȋṕšᶙṁ ḍỡḽǭᵳ ʂǐť ӓṁệẗ, ĉṓɲṩḙċťᶒțûɾ ấɖḯƥĭṩčįɳġ ḝłįʈ, șếᶑ ᶁⱺ ẽḭŭŝḿꝋď ṫĕᶆᶈṓɍ ỉñḉīḑȋᵭṵńť ṷŧ ḹẩḇőꝛế éȶ đꝍꞎôꝛȇ ᵯáꞡᶇā ąⱡîɋṹẵ.'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5D8j2
function name:  (null)
number of ops:  36
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'find_similar3'
          2        SEND_VAL                                                 'i+knew+you+love+me'
          3        SEND_VAL                                                 'finally++i+know+you+loved+me+and+all+my+pets'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
   79     7        INIT_FCALL                                               'var_dump'
          8        INIT_FCALL                                               'find_similar3'
          9        SEND_VAL                                                 'I+you+love'
         10        SEND_VAL                                                 'This+is+a+demo+text+and+I+love+you+about+this'
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR                                                 $2
         13        DO_ICALL                                                 
   80    14        INIT_FCALL                                               'var_dump'
         15        INIT_FCALL                                               'find_similar3'
         16        SEND_VAL                                                 'a+unik+idia'
         17        SEND_VAL                                                 'I+have+a+unique+idea.+Do+you+need%3F'
         18        DO_FCALL                                      0  $4      
         19        SEND_VAR                                                 $4
         20        DO_ICALL                                                 
   81    21        INIT_FCALL                                               'var_dump'
         22        INIT_FCALL                                               'find_similar3'
         23        SEND_VAL                                                 'Goebel%2C+Weiss%2C+Goethe%2C+Goethe+und+Goetz'
         24        SEND_VAL                                                 'Wei%C3%9F%2C+Goldmann%2C+G%C3%B6bel%2C+Weiss%2C+G%C3%B6the%2C+Goethe+und+G%C3%B6tz'
         25        DO_FCALL                                      0  $6      
         26        SEND_VAR                                                 $6
         27        DO_ICALL                                                 
   82    28        INIT_FCALL                                               'var_dump'
         29        INIT_FCALL                                               'find_similar3'
         30        SEND_VAL                                                 '%E1%B8%BC%C6%A1%E1%B6%89%C3%AB%E1%B6%86+%C8%8B%E1%B9%95%C5%A1%E1%B6%99%E1%B9%81+%E1%B8%8D%E1%BB%A1%E1%B8%BD%C7%AD%E1%B5%B3+%CA%82%C7%90%C5%A5+%D3%93%E1%B9%81%E1%BB%87%E1%BA%97%2C+%C4%89%E1%B9%93%C9%B2%E1%B9%A9%E1%B8%99%C4%8B%C5%A5%E1%B6%92%C8%9B%C3%BB%C9%BE+%E1%BA%A5%C9%96%E1%B8%AF%C6%A5%C4%AD%E1%B9%A9%C4%8D%C4%AF%C9%B3%C4%A1+%E1%B8%9D%C5%82%C4%AF%CA%88'
   83    31        SEND_VAL                                                 '%E1%B8%BC%C6%A1%E1%B6%89%C3%AB%E1%B6%86+%C8%8B%E1%B9%95%C5%A1%E1%B6%99%E1%B9%81+%E1%B8%8D%E1%BB%A1%E1%B8%BD%C7%AD%E1%B5%B3+%CA%82%C7%90%C5%A5+%D3%93%E1%B9%81%E1%BB%87%E1%BA%97%2C+%C4%89%E1%B9%93%C9%B2%E1%B9%A9%E1%B8%99%C4%8B%C5%A5%E1%B6%92%C8%9B%C3%BB%C9%BE+%E1%BA%A5%C9%96%E1%B8%AF%C6%A5%C4%AD%E1%B9%A9%C4%8D%C4%AF%C9%B3%C4%A1+%E1%B8%9D%C5%82%C4%AF%CA%88%2C+%C8%99%E1%BA%BF%E1%B6%91+%E1%B6%81%E2%B1%BA+%E1%BA%BD%E1%B8%AD%C5%AD%C5%9D%E1%B8%BF%EA%9D%8B%C4%8F+%E1%B9%AB%C4%95%E1%B6%86%E1%B6%88%E1%B9%93%C9%8D+%E1%BB%89%C3%B1%E1%B8%89%C4%AB%E1%B8%91%C8%8B%E1%B5%AD%E1%B9%B5%C5%84%C5%A5+%E1%B9%B7%C5%A7+%E1%B8%B9%E1%BA%A9%E1%B8%87%C5%91%EA%9D%9B%E1%BA%BF+%C3%A9%C8%B6+%C4%91%EA%9D%8D%EA%9E%8E%C3%B4%EA%9D%9B%C8%87+%E1%B5%AF%C3%A1%EA%9E%A1%E1%B6%87%C4%81+%C4%85%E2%B1%A1%C3%AE%C9%8B%E1%B9%B9%E1%BA%B5.'
         32        DO_FCALL                                      0  $8      
         33        SEND_VAR                                                 $8
         34        DO_ICALL                                                 
         35      > RETURN                                                   1

Function find_similar3:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 11
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 47, Position 2 = 139
Branch analysis from position: 47
2 jumps found. (Code = 78) Position 1 = 48, Position 2 = 139
Branch analysis from position: 48
2 jumps found. (Code = 77) Position 1 = 50, Position 2 = 90
Branch analysis from position: 50
2 jumps found. (Code = 78) Position 1 = 51, Position 2 = 90
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 54, Position 2 = 77
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 63
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
2 jumps found. (Code = 43) Position 1 = 67, Position 2 = 74
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 79
Branch analysis from position: 79
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 74
1 jumps found. (Code = 42) Position 1 = 79
Branch analysis from position: 79
Branch analysis from position: 63
2 jumps found. (Code = 43) Position 1 = 67, Position 2 = 74
Branch analysis from position: 67
Branch analysis from position: 74
Branch analysis from position: 77
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 90
2 jumps found. (Code = 77) Position 1 = 104, Position 2 = 122
Branch analysis from position: 104
2 jumps found. (Code = 78) Position 1 = 105, Position 2 = 122
Branch analysis from position: 105
2 jumps found. (Code = 43) Position 1 = 119, Position 2 = 120
Branch analysis from position: 119
1 jumps found. (Code = 42) Position 1 = 104
Branch analysis from position: 104
Branch analysis from position: 120
Branch analysis from position: 122
2 jumps found. (Code = 43) Position 1 = 125, Position 2 = 132
Branch analysis from position: 125
1 jumps found. (Code = 42) Position 1 = 136
Branch analysis from position: 136
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 132
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 122
Branch analysis from position: 90
Branch analysis from position: 139
2 jumps found. (Code = 43) Position 1 = 142, Position 2 = 145
Branch analysis from position: 142
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 145
Branch analysis from position: 139
Branch analysis from position: 9
filename:       /in/5D8j2
function name:  find_similar3
number of ops:  151
compiled vars:  !0 = $needle, !1 = $str, !2 = $keep_needle_order, !3 = $valid, !4 = $encoding_s, !5 = $encoding_n, !6 = $pneed, !7 = $pstr, !8 = $word, !9 = $k, !10 = $w, !11 = $key, !12 = $tmp_word, !13 = $tmp_w, !14 = $tmp, !15 = $keys, !16 = $nominees, !17 = $idx, !18 = $nominee, !19 = $answer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <false>
    5     3        TYPE_CHECK                                   64  ~20     !0
          4        BOOL_NOT                                         ~21     ~20
          5      > JMPNZ_EX                                         ~21     ~21, ->9
          6    >   TYPE_CHECK                                   64  ~22     !1
          7        BOOL_NOT                                         ~23     ~22
          8        BOOL                                             ~21     ~23
          9    > > JMPZ                                                     ~21, ->11
    7    10    > > RETURN                                                   <false>
    9    11    >   ASSIGN                                                   !3, <array>
   11    12        INIT_FCALL                                               'setlocale'
         13        SEND_VAL                                                 0
         14        SEND_VAL                                                 'en_GB.UTF8'
         15        DO_ICALL                                                 
   12    16        INIT_FCALL                                               'mb_detect_encoding'
         17        SEND_VAR                                                 !1
         18        DO_ICALL                                         $26     
         19        ASSIGN                                                   !4, $26
   13    20        INIT_FCALL                                               'mb_detect_encoding'
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $28     
         23        ASSIGN                                                   !5, $28
   15    24        INIT_FCALL                                               'mb_regex_encoding'
         25        SEND_VAR                                                 !5
         26        DO_ICALL                                                 
   16    27        INIT_FCALL                                               'array_filter'
         28        INIT_FCALL                                               'mb_split'
         29        SEND_VAL                                                 '%5CW'
         30        SEND_VAR                                                 !0
         31        DO_ICALL                                         $31     
         32        SEND_VAR                                                 $31
         33        DO_ICALL                                         $32     
         34        ASSIGN                                                   !6, $32
   18    35        INIT_FCALL                                               'mb_regex_encoding'
         36        SEND_VAR                                                 !4
         37        DO_ICALL                                                 
   19    38        INIT_FCALL                                               'array_filter'
         39        INIT_FCALL                                               'mb_split'
         40        SEND_VAL                                                 '%5CW'
         41        SEND_VAR                                                 !1
         42        DO_ICALL                                         $35     
         43        SEND_VAR                                                 $35
         44        DO_ICALL                                         $36     
         45        ASSIGN                                                   !7, $36
   23    46      > FE_RESET_R                                       $38     !6, ->139
         47    > > FE_FETCH_R                                       ~39     $38, !8, ->139
         48    >   ASSIGN                                                   !9, ~39
   25    49      > FE_RESET_R                                       $41     !7, ->90
         50    > > FE_FETCH_R                                       ~42     $41, !10, ->90
         51    >   ASSIGN                                                   !11, ~42
   27    52        IS_NOT_IDENTICAL                                         !5, !4
         53      > JMPZ                                                     ~44, ->77
   31    54    >   IS_NOT_IDENTICAL                                         !5, 'ASCII'
         55      > JMPZ                                                     ~45, ->63
         56    >   INIT_FCALL                                               'mb_convert_encoding'
         57        SEND_VAR                                                 !8
         58        SEND_VAR                                                 !5
         59        SEND_VAL                                                 'ASCII'
         60        DO_ICALL                                         $46     
         61        QM_ASSIGN                                        ~47     $46
         62      > JMP                                                      ->64
         63    >   QM_ASSIGN                                        ~47     !8
         64    >   ASSIGN                                                   !12, ~47
   32    65        IS_NOT_IDENTICAL                                         !4, 'ASCII'
         66      > JMPZ                                                     ~49, ->74
         67    >   INIT_FCALL                                               'mb_convert_encoding'
         68        SEND_VAR                                                 !10
         69        SEND_VAR                                                 !4
         70        SEND_VAL                                                 'ASCII'
         71        DO_ICALL                                         $50     
         72        QM_ASSIGN                                        ~51     $50
         73      > JMP                                                      ->75
         74    >   QM_ASSIGN                                        ~51     !10
         75    >   ASSIGN                                                   !13, ~51
         76      > JMP                                                      ->79
   35    77    >   ASSIGN                                                   !12, !8
   36    78        ASSIGN                                                   !13, !10
   39    79    >   INIT_FCALL                                               'levenshtein'
         80        SEND_VAR                                                 !13
         81        SEND_VAR                                                 !12
         82        DO_ICALL                                         $56     
         83        ASSIGN_DIM                                               !14, !13
         84        OP_DATA                                                  $56
   40    85        INIT_ARRAY                                       ~58     !11
         86        ADD_ARRAY_ELEMENT                                ~58     !10
         87        ASSIGN_DIM                                               !15, !13
         88        OP_DATA                                                  ~58
   25    89      > JMP                                                      ->50
         90    >   FE_FREE                                                  $41
   44    91        INIT_FCALL                                               'array_flip'
         92        INIT_FCALL                                               'array_keys'
         93        SEND_VAR                                                 !14
         94        INIT_FCALL                                               'min'
         95        SEND_VAR                                                 !14
         96        DO_ICALL                                         $59     
         97        SEND_VAR                                                 $59
         98        DO_ICALL                                         $60     
         99        SEND_VAR                                                 $60
        100        DO_ICALL                                         $61     
        101        ASSIGN                                                   !16, $61
   45   102        ASSIGN                                                   !14, 10000
   46   103      > FE_RESET_R                                       $64     !16, ->122
        104    > > FE_FETCH_R                                       ~65     $64, !17, ->122
        105    >   ASSIGN                                                   !18, ~65
   48   106        INIT_FCALL                                               'levenshtein'
        107        INIT_FCALL                                               'metaphone'
        108        SEND_VAR                                                 !18
        109        DO_ICALL                                         $67     
        110        SEND_VAR                                                 $67
        111        INIT_FCALL                                               'metaphone'
        112        SEND_VAR                                                 !12
        113        DO_ICALL                                         $68     
        114        SEND_VAR                                                 $68
        115        DO_ICALL                                         $69     
        116        ASSIGN                                                   !17, $69
   49   117        IS_SMALLER                                               !17, !14
        118      > JMPZ                                                     ~71, ->120
   50   119    >   ASSIGN                                                   !19, !18
   53   120    >   UNSET_DIM                                                !16, !18
   46   121      > JMP                                                      ->104
        122    >   FE_FREE                                                  $64
   55   123        BOOL_NOT                                         ~73     !2
        124      > JMPZ                                                     ~73, ->132
   56   125    >   FETCH_DIM_R                                      ~74     !15, !19
        126        FETCH_DIM_R                                      ~75     ~74, 0
        127        FETCH_DIM_R                                      ~77     !15, !19
        128        FETCH_DIM_R                                      ~78     ~77, 1
        129        ASSIGN_DIM                                               !3, ~75
        130        OP_DATA                                                  ~78
        131      > JMP                                                      ->136
   59   132    >   FETCH_DIM_R                                      ~80     !15, !19
        133        FETCH_DIM_R                                      ~81     ~80, 1
        134        ASSIGN_DIM                                               !3, !9
        135        OP_DATA                                                  ~81
   61   136    >   ASSIGN                                           ~82     !16, <array>
        137        ASSIGN                                                   !14, ~82
   23   138      > JMP                                                      ->47
        139    >   FE_FREE                                                  $38
   63   140        BOOL_NOT                                         ~84     !2
        141      > JMPZ                                                     ~84, ->145
   65   142    >   INIT_FCALL                                               'ksort'
        143        SEND_REF                                                 !3
        144        DO_ICALL                                                 
   68   145    >   INIT_FCALL                                               'array_values'
        146        SEND_VAR                                                 !3
        147        DO_ICALL                                         $86     
        148        ASSIGN                                                   !3, $86
   71   149      > RETURN                                                   !3
   73   150*     > RETURN                                                   null

End of function find_similar3

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
191.15 ms | 1419 KiB | 46 Q