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

End of function comparewords

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

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.81 ms | 1419 KiB | 47 Q