3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Performs a words comparision between $s1 and $s2. This function counts every word only once, so 'hello hello' * would be 100% similar to 'hello'. Both strings will get converted to lowercase, with its diacritical marks * (´, ¨, ^, `, ~, etc.) stripped out. This behaviour is very much like a 'keywords search'. * @param string $s1 First string to compare * @param string $s2 Second string to compare * @param null|string[] $skipWords Value-only array of words that should'nt be taken into account when comparing $s1 and $s2 * @param null|string[] $skipText Value-only array of text that should'nt be taken into account when comparing $s1 and $s2. * Please note that this text will be stripped from the end, start and middle parts of words. * @return float|int Percent of similarity between $s1 and $s2, where 1 represents 100% and 0 represents 0% */ function compareWords($s1, $s2, $skipWords = [ 'en', 'de', 'del', 'los', 'la', 'in', 'from', 'the' ], $skipText = [ '.', ',', ';', ':' ]) { if ($s1 === null || $s2 === null) return 0; if ($skipText !== null && count($skipText) > 0) { $s1 = str_replace($skipText, '', $s1); $s2 = str_replace($skipText, '', $s2); } if ($skipWords !== null && count($skipWords) > 0) { $skipWords = array_map(function ($item) { return preg_quote($item, '/'); }, $skipWords); $skipWordsRegex = '/(?:(?<=\s)|^)(?:' . implode('|', $skipWords) . ')(?:(?=\s)|$)/'; $s1 = preg_replace($skipWordsRegex, '', $s1); $s2 = preg_replace($skipWordsRegex, '', $s2); } $s1 = trim(UString::lowerCase(preg_replace('/\s+/', ' ', UString::removeDiacritics($s1)))); $s2 = trim(UString::lowerCase(preg_replace('/\s+/', ' ', UString::removeDiacritics($s2)))); if (strlen($s1) === 0 || strlen($s2) === 0) return 0; $s1Words = array_unique(explode(' ', $s1)); $s2Words = array_unique(explode(' ', $s2)); $s1WordsCount = count($s1Words); $s2WordsCount = count($s2Words); // make sure $s1Words is the smaller array, to have a smaller cycle if ($s1WordsCount > $s2WordsCount) { $temp = $s1Words; $s1Words = $s2Words; $s2Words = $temp; } $s2Words = array_flip($s2Words); $maxWords = max($s1WordsCount, $s2WordsCount); $matches = 0; foreach ($s1Words as $s1Word) { if (array_key_exists($s1Word, $s2Words)) $matches++; } return $matches / $maxWords; } var_dump(compareWords('The tomato sauce.', 'tomato sauce'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0jqJI
function name:  (null)
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'comparewords'
          2        SEND_VAL                                                 'The+tomato+sauce.'
          3        SEND_VAL                                                 'tomato+sauce'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function comparewords:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 46) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 28
Branch analysis from position: 16
2 jumps found. (Code = 46) Position 1 = 30, Position 2 = 33
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 59
Branch analysis from position: 34
2 jumps found. (Code = 47) Position 1 = 92, Position 2 = 95
Branch analysis from position: 92
2 jumps found. (Code = 43) Position 1 = 96, Position 2 = 97
Branch analysis from position: 96
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 97
2 jumps found. (Code = 43) Position 1 = 119, Position 2 = 122
Branch analysis from position: 119
2 jumps found. (Code = 77) Position 1 = 133, Position 2 = 138
Branch analysis from position: 133
2 jumps found. (Code = 78) Position 1 = 134, Position 2 = 138
Branch analysis from position: 134
2 jumps found. (Code = 43) Position 1 = 136, Position 2 = 137
Branch analysis from position: 136
1 jumps found. (Code = 42) Position 1 = 133
Branch analysis from position: 133
Branch analysis from position: 137
Branch analysis from position: 138
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 138
Branch analysis from position: 122
Branch analysis from position: 95
Branch analysis from position: 59
Branch analysis from position: 33
Branch analysis from position: 28
Branch analysis from position: 15
Branch analysis from position: 8
filename:       /in/0jqJI
function name:  compareWords
number of ops:  142
compiled vars:  !0 = $s1, !1 = $s2, !2 = $skipWords, !3 = $skipText, !4 = $skipWordsRegex, !5 = $s1Words, !6 = $s2Words, !7 = $s1WordsCount, !8 = $s2WordsCount, !9 = $temp, !10 = $maxWords, !11 = $matches, !12 = $s1Word
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <array>
          3        RECV_INIT                                        !3      <array>
   20     4        TYPE_CHECK                                    2  ~13     !0
          5      > JMPNZ_EX                                         ~13     ~13, ->8
          6    >   TYPE_CHECK                                    2  ~14     !1
          7        BOOL                                             ~13     ~14
          8    > > JMPZ                                                     ~13, ->10
          9    > > RETURN                                                   0
   22    10    >   TYPE_CHECK                                  1020  ~15     !3
         11      > JMPZ_EX                                          ~15     ~15, ->15
         12    >   COUNT                                            ~16     !3
         13        IS_SMALLER                                       ~17     0, ~16
         14        BOOL                                             ~15     ~17
         15    > > JMPZ                                                     ~15, ->28
   23    16    >   INIT_FCALL                                               'str_replace'
         17        SEND_VAR                                                 !3
         18        SEND_VAL                                                 ''
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                         $18     
         21        ASSIGN                                                   !0, $18
   24    22        INIT_FCALL                                               'str_replace'
         23        SEND_VAR                                                 !3
         24        SEND_VAL                                                 ''
         25        SEND_VAR                                                 !1
         26        DO_ICALL                                         $20     
         27        ASSIGN                                                   !1, $20
   27    28    >   TYPE_CHECK                                  1020  ~22     !2
         29      > JMPZ_EX                                          ~22     ~22, ->33
         30    >   COUNT                                            ~23     !2
         31        IS_SMALLER                                       ~24     0, ~23
         32        BOOL                                             ~22     ~24
         33    > > JMPZ                                                     ~22, ->59
   28    34    >   INIT_FCALL                                               'array_map'
         35        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F0jqJI%3A28%240'
         36        SEND_VAL                                                 ~25
         37        SEND_VAR                                                 !2
         38        DO_ICALL                                         $26     
         39        ASSIGN                                                   !2, $26
   29    40        INIT_FCALL                                               'implode'
         41        SEND_VAL                                                 '%7C'
         42        SEND_VAR                                                 !2
         43        DO_ICALL                                         $28     
         44        CONCAT                                           ~29     '%2F%28%3F%3A%28%3F%3C%3D%5Cs%29%7C%5E%29%28%3F%3A', $28
         45        CONCAT                                           ~30     ~29, '%29%28%3F%3A%28%3F%3D%5Cs%29%7C%24%29%2F'
         46        ASSIGN                                                   !4, ~30
   30    47        INIT_FCALL                                               'preg_replace'
         48        SEND_VAR                                                 !4
         49        SEND_VAL                                                 ''
         50        SEND_VAR                                                 !0
         51        DO_ICALL                                         $32     
         52        ASSIGN                                                   !0, $32
   31    53        INIT_FCALL                                               'preg_replace'
         54        SEND_VAR                                                 !4
         55        SEND_VAL                                                 ''
         56        SEND_VAR                                                 !1
         57        DO_ICALL                                         $34     
         58        ASSIGN                                                   !1, $34
   34    59    >   INIT_FCALL                                               'trim'
         60        INIT_STATIC_METHOD_CALL                                  'UString', 'lowerCase'
         61        INIT_FCALL                                               'preg_replace'
         62        SEND_VAL                                                 '%2F%5Cs%2B%2F'
         63        SEND_VAL                                                 '+'
         64        INIT_STATIC_METHOD_CALL                                  'UString', 'removeDiacritics'
         65        SEND_VAR_EX                                              !0
         66        DO_FCALL                                      0  $36     
         67        SEND_VAR                                                 $36
         68        DO_ICALL                                         $37     
         69        SEND_VAR_NO_REF_EX                                       $37
         70        DO_FCALL                                      0  $38     
         71        SEND_VAR                                                 $38
         72        DO_ICALL                                         $39     
         73        ASSIGN                                                   !0, $39
   35    74        INIT_FCALL                                               'trim'
         75        INIT_STATIC_METHOD_CALL                                  'UString', 'lowerCase'
         76        INIT_FCALL                                               'preg_replace'
         77        SEND_VAL                                                 '%2F%5Cs%2B%2F'
         78        SEND_VAL                                                 '+'
         79        INIT_STATIC_METHOD_CALL                                  'UString', 'removeDiacritics'
         80        SEND_VAR_EX                                              !1
         81        DO_FCALL                                      0  $41     
         82        SEND_VAR                                                 $41
         83        DO_ICALL                                         $42     
         84        SEND_VAR_NO_REF_EX                                       $42
         85        DO_FCALL                                      0  $43     
         86        SEND_VAR                                                 $43
         87        DO_ICALL                                         $44     
         88        ASSIGN                                                   !1, $44
   37    89        STRLEN                                           ~46     !0
         90        IS_IDENTICAL                                     ~47     ~46, 0
         91      > JMPNZ_EX                                         ~47     ~47, ->95
         92    >   STRLEN                                           ~48     !1
         93        IS_IDENTICAL                                     ~49     ~48, 0
         94        BOOL                                             ~47     ~49
         95    > > JMPZ                                                     ~47, ->97
         96    > > RETURN                                                   0
   39    97    >   INIT_FCALL                                               'array_unique'
         98        INIT_FCALL                                               'explode'
         99        SEND_VAL                                                 '+'
        100        SEND_VAR                                                 !0
        101        DO_ICALL                                         $50     
        102        SEND_VAR                                                 $50
        103        DO_ICALL                                         $51     
        104        ASSIGN                                                   !5, $51
   40   105        INIT_FCALL                                               'array_unique'
        106        INIT_FCALL                                               'explode'
        107        SEND_VAL                                                 '+'
        108        SEND_VAR                                                 !1
        109        DO_ICALL                                         $53     
        110        SEND_VAR                                                 $53
        111        DO_ICALL                                         $54     
        112        ASSIGN                                                   !6, $54
   42   113        COUNT                                            ~56     !5
        114        ASSIGN                                                   !7, ~56
   43   115        COUNT                                            ~58     !6
        116        ASSIGN                                                   !8, ~58
   46   117        IS_SMALLER                                               !8, !7
        118      > JMPZ                                                     ~60, ->122
   47   119    >   ASSIGN                                                   !9, !5
   48   120        ASSIGN                                                   !5, !6
   49   121        ASSIGN                                                   !6, !9
   52   122    >   INIT_FCALL                                               'array_flip'
        123        SEND_VAR                                                 !6
        124        DO_ICALL                                         $64     
        125        ASSIGN                                                   !6, $64
   54   126        INIT_FCALL                                               'max'
        127        SEND_VAR                                                 !7
        128        SEND_VAR                                                 !8
        129        DO_ICALL                                         $66     
        130        ASSIGN                                                   !10, $66
   55   131        ASSIGN                                                   !11, 0
   57   132      > FE_RESET_R                                       $69     !5, ->138
        133    > > FE_FETCH_R                                               $69, !12, ->138
   58   134    >   ARRAY_KEY_EXISTS                                         !12, !6
        135      > JMPZ                                                     ~70, ->137
        136    >   PRE_INC                                                  !11
   57   137    > > JMP                                                      ->133
        138    >   FE_FREE                                                  $69
   61   139        DIV                                              ~72     !11, !10
        140      > RETURN                                                   ~72
   62   141*     > RETURN                                                   null

End of function comparewords

Function %00%7Bclosure%7D%2Fin%2F0jqJI%3A28%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0jqJI
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        INIT_FCALL                                               'preg_quote'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 '%2F'
          4        DO_ICALL                                         $1      
          5      > RETURN                                                   $1
          6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F0jqJI%3A28%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
190.6 ms | 1411 KiB | 36 Q