3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getNgrams($match, $n = 2) { $nGrams = []; for ($pos = strlen($match) - $n; $pos >= 0; --$pos) { $nGrams[] = substr($match, $pos, $n); } return $nGrams; } $question = 'happy birthday'; $questionNGrams = array_map("getNgrams", explode(' ', $question)); $rta = ['today', 'happy', ' tomorrow ', 'monday', 'birthda']; foreach ($rta as $value) { $rtaNGram = getNgrams($value); $rtaNGramCount = count($rtaNGram); foreach ($questionNGrams as $questionNGram) { if (count(array_intersect($rtaNGram, $questionNGram)) / $rtaNGramCount >= 0.8) { echo "{$value}<br>"; break; } } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 36
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 36
Branch analysis from position: 13
2 jumps found. (Code = 77) Position 1 = 20, Position 2 = 34
Branch analysis from position: 20
2 jumps found. (Code = 78) Position 1 = 21, Position 2 = 34
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 33
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
Branch analysis from position: 34
Branch analysis from position: 34
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
filename:       /in/nlLFE
function name:  (null)
number of ops:  38
compiled vars:  !0 = $question, !1 = $questionNGrams, !2 = $rta, !3 = $value, !4 = $rtaNGram, !5 = $rtaNGramCount, !6 = $questionNGram
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   ASSIGN                                                   !0, 'happy+birthday'
   11     1        INIT_FCALL                                               'array_map'
          2        SEND_VAL                                                 'getNgrams'
          3        INIT_FCALL                                               'explode'
          4        SEND_VAL                                                 '+'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $8      
          7        SEND_VAR                                                 $8
          8        DO_ICALL                                         $9      
          9        ASSIGN                                                   !1, $9
   13    10        ASSIGN                                                   !2, <array>
   15    11      > FE_RESET_R                                       $12     !2, ->36
         12    > > FE_FETCH_R                                               $12, !3, ->36
   16    13    >   INIT_FCALL                                               'getngrams'
         14        SEND_VAR                                                 !3
         15        DO_FCALL                                      0  $13     
         16        ASSIGN                                                   !4, $13
   17    17        COUNT                                            ~15     !4
         18        ASSIGN                                                   !5, ~15
   18    19      > FE_RESET_R                                       $17     !1, ->34
         20    > > FE_FETCH_R                                               $17, !6, ->34
   19    21    >   INIT_FCALL                                               'array_intersect'
         22        SEND_VAR                                                 !4
         23        SEND_VAR                                                 !6
         24        DO_ICALL                                         $18     
         25        COUNT                                            ~19     $18
         26        DIV                                              ~20     ~19, !5
         27        IS_SMALLER_OR_EQUAL                                      0.8, ~20
         28      > JMPZ                                                     ~21, ->33
   20    29    >   NOP                                                      
         30        FAST_CONCAT                                      ~22     !3, '%3Cbr%3E'
         31        ECHO                                                     ~22
   21    32      > JMP                                                      ->34
   18    33    > > JMP                                                      ->20
         34    >   FE_FREE                                                  $17
   15    35      > JMP                                                      ->12
         36    >   FE_FREE                                                  $12
   24    37      > RETURN                                                   1

Function getngrams:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 7
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 7
Branch analysis from position: 17
Branch analysis from position: 7
filename:       /in/nlLFE
function name:  getNgrams
number of ops:  19
compiled vars:  !0 = $match, !1 = $n, !2 = $nGrams, !3 = $pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      2
    3     2        ASSIGN                                                   !2, <array>
    4     3        STRLEN                                           ~5      !0
          4        SUB                                              ~6      ~5, !1
          5        ASSIGN                                                   !3, ~6
          6      > JMP                                                      ->15
    5     7    >   INIT_FCALL                                               'substr'
          8        SEND_VAR                                                 !0
          9        SEND_VAR                                                 !3
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                         $9      
         12        ASSIGN_DIM                                               !2
         13        OP_DATA                                                  $9
    4    14        PRE_DEC                                                  !3
         15    >   IS_SMALLER_OR_EQUAL                                      0, !3
         16      > JMPNZ                                                    ~11, ->7
    7    17    > > RETURN                                                   !2
    8    18*     > RETURN                                                   null

End of function getngrams

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.11 ms | 1403 KiB | 22 Q