3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @param string $string The string from which the diacritic marks (¨, ´, ~, `, ^, etc.) will be removed. * @return string String without diacritic marks. */ function removeDiacritics($string) { return transliterator_transliterate('Any-Latin;Latin-ASCII', $string); } /** * Ensures that $string is UTF-8 encoded. * @param string $string The string to ensure encoding * @return string The UTF-8 encoded string * @throws Exception If $string has no valid encoding */ function ensureUTF8Encoding($string) { $encoding = mb_detect_encoding($string, 'UTF-8', true); if ($encoding === false) { $encoding = mb_detect_encoding($string, 'auto'); } if ($encoding === false) { throw new Exception('No valid encoding detected'); } return $encoding !== 'UTF-8' ? mb_convert_encoding($string, 'UTF-8', $encoding) : $string; } /** * @param string $string The string to get lower-cased * @return string Lower-cased and UTF-8 encoded value of $string */ function lowerCase($string) { return mb_strtolower(ensureUTF8Encoding($string), 'UTF-8'); } /** * 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); } $s1 = trim(lowerCase(preg_replace('/\s+/', ' ', removeDiacritics($s1)))); $s2 = trim(lowerCase(preg_replace('/\s+/', ' ', removeDiacritics($s2)))); if (strlen($s1) === 0 || strlen($s2) === 0) return 0; if ($skipWords !== null && count($skipWords) > 0) { $skipWords = array_map(function ($item) { return preg_quote($item, '/'); }, $skipWords); $skipWordsRegex = '/(?:(?<=\s)|^)(?:' . implode('|', $skipWords) . ')(?:(?=\s)|$)/'; $s1 = trim(preg_replace([$skipWordsRegex, '/\s+/'], ['', ' '], $s1)); $s2 = trim(preg_replace([$skipWordsRegex, '/\s+/'], ['', ' '], $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/uCHUg
function name:  (null)
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   99     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 removediacritics:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/uCHUg
function name:  removeDiacritics
number of ops:  7
compiled vars:  !0 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
    8     1        INIT_FCALL_BY_NAME                                       'transliterator_transliterate'
          2        SEND_VAL_EX                                              'Any-Latin%3BLatin-ASCII'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $1      
          5      > RETURN                                                   $1
    9     6*     > RETURN                                                   null

End of function removediacritics

Function ensureutf8encoding:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 14
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 20
Branch analysis from position: 16
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/uCHUg
function name:  ensureUTF8Encoding
number of ops:  32
compiled vars:  !0 = $string, !1 = $encoding
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   18     1        INIT_FCALL                                               'mb_detect_encoding'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 'UTF-8'
          4        SEND_VAL                                                 <true>
          5        DO_ICALL                                         $2      
          6        ASSIGN                                                   !1, $2
   19     7        TYPE_CHECK                                    4          !1
          8      > JMPZ                                                     ~4, ->14
   20     9    >   INIT_FCALL                                               'mb_detect_encoding'
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 'auto'
         12        DO_ICALL                                         $5      
         13        ASSIGN                                                   !1, $5
   22    14    >   TYPE_CHECK                                    4          !1
         15      > JMPZ                                                     ~7, ->20
   23    16    >   NEW                                              $8      'Exception'
         17        SEND_VAL_EX                                              'No+valid+encoding+detected'
         18        DO_FCALL                                      0          
         19      > THROW                                         0          $8
   25    20    >   IS_NOT_IDENTICAL                                         !1, 'UTF-8'
         21      > JMPZ                                                     ~10, ->29
         22    >   INIT_FCALL                                               'mb_convert_encoding'
         23        SEND_VAR                                                 !0
         24        SEND_VAL                                                 'UTF-8'
         25        SEND_VAR                                                 !1
         26        DO_ICALL                                         $11     
         27        QM_ASSIGN                                        ~12     $11
         28      > JMP                                                      ->30
         29    >   QM_ASSIGN                                        ~12     !0
         30    > > RETURN                                                   ~12
   26    31*     > RETURN                                                   null

End of function ensureutf8encoding

Function lowercase:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/uCHUg
function name:  lowerCase
number of ops:  10
compiled vars:  !0 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   33     1        INIT_FCALL                                               'mb_strtolower'
          2        INIT_FCALL                                               'ensureutf8encoding'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $1      
          5        SEND_VAR                                                 $1
          6        SEND_VAL                                                 'UTF-8'
          7        DO_ICALL                                         $2      
          8      > RETURN                                                   $2
   34     9*     > RETURN                                                   null

End of function lowercase

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 = 47) Position 1 = 61, Position 2 = 64
Branch analysis from position: 61
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 66
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 66
2 jumps found. (Code = 46) Position 1 = 68, Position 2 = 71
Branch analysis from position: 68
2 jumps found. (Code = 43) Position 1 = 72, Position 2 = 107
Branch analysis from position: 72
2 jumps found. (Code = 47) Position 1 = 110, Position 2 = 113
Branch analysis from position: 110
2 jumps found. (Code = 43) Position 1 = 114, Position 2 = 115
Branch analysis from position: 114
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 115
2 jumps found. (Code = 43) Position 1 = 137, Position 2 = 140
Branch analysis from position: 137
2 jumps found. (Code = 77) Position 1 = 151, Position 2 = 156
Branch analysis from position: 151
2 jumps found. (Code = 78) Position 1 = 152, Position 2 = 156
Branch analysis from position: 152
2 jumps found. (Code = 43) Position 1 = 154, Position 2 = 155
Branch analysis from position: 154
1 jumps found. (Code = 42) Position 1 = 151
Branch analysis from position: 151
Branch analysis from position: 155
Branch analysis from position: 156
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 156
Branch analysis from position: 140
Branch analysis from position: 113
Branch analysis from position: 107
Branch analysis from position: 71
Branch analysis from position: 64
Branch analysis from position: 28
Branch analysis from position: 15
Branch analysis from position: 8
filename:       /in/uCHUg
function name:  compareWords
number of ops:  160
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
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <array>
          3        RECV_INIT                                        !3      <array>
   53     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
   55    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
   56    16    >   INIT_FCALL                                               'str_replace'
         17        SEND_VAR                                                 !3
         18        SEND_VAL                                                 ''
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                         $18     
         21        ASSIGN                                                   !0, $18
   57    22        INIT_FCALL                                               'str_replace'
         23        SEND_VAR                                                 !3
         24        SEND_VAL                                                 ''
         25        SEND_VAR                                                 !1
         26        DO_ICALL                                         $20     
         27        ASSIGN                                                   !1, $20
   60    28    >   INIT_FCALL                                               'trim'
         29        INIT_FCALL                                               'lowercase'
         30        INIT_FCALL                                               'preg_replace'
         31        SEND_VAL                                                 '%2F%5Cs%2B%2F'
         32        SEND_VAL                                                 '+'
         33        INIT_FCALL                                               'removediacritics'
         34        SEND_VAR                                                 !0
         35        DO_FCALL                                      0  $22     
         36        SEND_VAR                                                 $22
         37        DO_ICALL                                         $23     
         38        SEND_VAR                                                 $23
         39        DO_FCALL                                      0  $24     
         40        SEND_VAR                                                 $24
         41        DO_ICALL                                         $25     
         42        ASSIGN                                                   !0, $25
   61    43        INIT_FCALL                                               'trim'
         44        INIT_FCALL                                               'lowercase'
         45        INIT_FCALL                                               'preg_replace'
         46        SEND_VAL                                                 '%2F%5Cs%2B%2F'
         47        SEND_VAL                                                 '+'
         48        INIT_FCALL                                               'removediacritics'
         49        SEND_VAR                                                 !1
         50        DO_FCALL                                      0  $27     
         51        SEND_VAR                                                 $27
         52        DO_ICALL                                         $28     
         53        SEND_VAR                                                 $28
         54        DO_FCALL                                      0  $29     
         55        SEND_VAR                                                 $29
         56        DO_ICALL                                         $30     
         57        ASSIGN                                                   !1, $30
   63    58        STRLEN                                           ~32     !0
         59        IS_IDENTICAL                                     ~33     ~32, 0
         60      > JMPNZ_EX                                         ~33     ~33, ->64
         61    >   STRLEN                                           ~34     !1
         62        IS_IDENTICAL                                     ~35     ~34, 0
         63        BOOL                                             ~33     ~35
         64    > > JMPZ                                                     ~33, ->66
         65    > > RETURN                                                   0
   65    66    >   TYPE_CHECK                                  1020  ~36     !2
         67      > JMPZ_EX                                          ~36     ~36, ->71
         68    >   COUNT                                            ~37     !2
         69        IS_SMALLER                                       ~38     0, ~37
         70        BOOL                                             ~36     ~38
         71    > > JMPZ                                                     ~36, ->107
   66    72    >   INIT_FCALL                                               'array_map'
         73        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FuCHUg%3A66%240'
         74        SEND_VAL                                                 ~39
         75        SEND_VAR                                                 !2
         76        DO_ICALL                                         $40     
         77        ASSIGN                                                   !2, $40
   67    78        INIT_FCALL                                               'implode'
         79        SEND_VAL                                                 '%7C'
         80        SEND_VAR                                                 !2
         81        DO_ICALL                                         $42     
         82        CONCAT                                           ~43     '%2F%28%3F%3A%28%3F%3C%3D%5Cs%29%7C%5E%29%28%3F%3A', $42
         83        CONCAT                                           ~44     ~43, '%29%28%3F%3A%28%3F%3D%5Cs%29%7C%24%29%2F'
         84        ASSIGN                                                   !4, ~44
   68    85        INIT_FCALL                                               'trim'
         86        INIT_FCALL                                               'preg_replace'
         87        INIT_ARRAY                                       ~46     !4
         88        ADD_ARRAY_ELEMENT                                ~46     '%2F%5Cs%2B%2F'
         89        SEND_VAL                                                 ~46
         90        SEND_VAL                                                 <array>
         91        SEND_VAR                                                 !0
         92        DO_ICALL                                         $47     
         93        SEND_VAR                                                 $47
         94        DO_ICALL                                         $48     
         95        ASSIGN                                                   !0, $48
   69    96        INIT_FCALL                                               'trim'
         97        INIT_FCALL                                               'preg_replace'
         98        INIT_ARRAY                                       ~50     !4
         99        ADD_ARRAY_ELEMENT                                ~50     '%2F%5Cs%2B%2F'
        100        SEND_VAL                                                 ~50
        101        SEND_VAL                                                 <array>
        102        SEND_VAR                                                 !1
        103        DO_ICALL                                         $51     
        104        SEND_VAR                                                 $51
        105        DO_ICALL                                         $52     
        106        ASSIGN                                                   !1, $52
   72   107    >   STRLEN                                           ~54     !0
        108        IS_IDENTICAL                                     ~55     ~54, 0
        109      > JMPNZ_EX                                         ~55     ~55, ->113
        110    >   STRLEN                                           ~56     !1
        111        IS_IDENTICAL                                     ~57     ~56, 0
        112        BOOL                                             ~55     ~57
        113    > > JMPZ                                                     ~55, ->115
        114    > > RETURN                                                   0
   74   115    >   INIT_FCALL                                               'array_unique'
        116        INIT_FCALL                                               'explode'
        117        SEND_VAL                                                 '+'
        118        SEND_VAR                                                 !0
        119        DO_ICALL                                         $58     
        120        SEND_VAR                                                 $58
        121        DO_ICALL                                         $59     
        122        ASSIGN                                                   !5, $59
   75   123        INIT_FCALL                                               'array_unique'
        124        INIT_FCALL                                               'explode'
        125        SEND_VAL                                                 '+'
        126        SEND_VAR                                                 !1
        127        DO_ICALL                                         $61     
        128        SEND_VAR                                                 $61
        129        DO_ICALL                                         $62     
        130        ASSIGN                                                   !6, $62
   77   131        COUNT                                            ~64     !5
        132        ASSIGN                                                   !7, ~64
   78   133        COUNT                                            ~66     !6
        134        ASSIGN                                                   !8, ~66
   81   135        IS_SMALLER                                               !8, !7
        136      > JMPZ                                                     ~68, ->140
   82   137    >   ASSIGN                                                   !9, !5
   83   138        ASSIGN                                                   !5, !6
   84   139        ASSIGN                                                   !6, !9
   87   140    >   INIT_FCALL                                               'array_flip'
        141        SEND_VAR                                                 !6
        142        DO_ICALL                                         $72     
        143        ASSIGN                                                   !6, $72
   89   144        INIT_FCALL                                               'max'
        145        SEND_VAR                                                 !7
        146        SEND_VAR                                                 !8
        147        DO_ICALL                                         $74     
        148        ASSIGN                                                   !10, $74
   90   149        ASSIGN                                                   !11, 0
   92   150      > FE_RESET_R                                       $77     !5, ->156
        151    > > FE_FETCH_R                                               $77, !12, ->156
   93   152    >   ARRAY_KEY_EXISTS                                         !12, !6
        153      > JMPZ                                                     ~78, ->155
        154    >   PRE_INC                                                  !11
   92   155    > > JMP                                                      ->151
        156    >   FE_FREE                                                  $77
   96   157        DIV                                              ~80     !11, !10
        158      > RETURN                                                   ~80
   97   159*     > RETURN                                                   null

End of function comparewords

Function %00%7Bclosure%7D%2Fin%2FuCHUg%3A66%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/uCHUg
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     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%2FuCHUg%3A66%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
216.4 ms | 1419 KiB | 50 Q