3v4l.org

run code in 300+ PHP versions simultaneously
<?php // input misspelled word $input = 'carrrot'; // array of words to check against $words = array('apple','pineapple','banana','orange', 'radish','carrot','pea','bean','potato'); // no shortest distance found, yet $shortest = -1; var_dump($words); // loop through words to find the closest foreach ($words as $word) { // calculate the distance between the input word, // and the current word $lev = levenshtein($input, $word); // check for an exact match if ($lev == 0) { // closest word is this one (exact match) $closest = $word; $shortest = 0; // break out of the loop; we've found an exact match break; } // if this distance is less than the next found shortest // distance, OR if a next shortest word has not yet been found if ($lev <= $shortest || $shortest < 0) { // set the closest match, and shortest distance $closest = $word; $shortest = $lev; } } echo "Input word: $input\n"; if ($shortest == 0) { echo "Exact match found: $closest\n"; } else { echo "Did you mean: $closest?\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 26
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 26
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 38
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 47) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 25
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 25
Branch analysis from position: 22
Branch analysis from position: 26
Branch analysis from position: 26
filename:       /in/FVSKU
function name:  (null)
number of ops:  43
compiled vars:  !0 = $input, !1 = $words, !2 = $shortest, !3 = $word, !4 = $lev, !5 = $closest
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'carrrot'
    6     1        ASSIGN                                                   !1, <array>
   10     2        ASSIGN                                                   !2, -1
   11     3        INIT_FCALL                                               'var_dump'
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                                 
   13     6      > FE_RESET_R                                       $10     !1, ->26
          7    > > FE_FETCH_R                                               $10, !3, ->26
   17     8    >   INIT_FCALL                                               'levenshtein'
          9        SEND_VAR                                                 !0
         10        SEND_VAR                                                 !3
         11        DO_ICALL                                         $11     
         12        ASSIGN                                                   !4, $11
   20    13        IS_EQUAL                                                 !4, 0
         14      > JMPZ                                                     ~13, ->18
   23    15    >   ASSIGN                                                   !5, !3
   24    16        ASSIGN                                                   !2, 0
   27    17      > JMP                                                      ->26
   32    18    >   IS_SMALLER_OR_EQUAL                              ~16     !4, !2
         19      > JMPNZ_EX                                         ~16     ~16, ->22
         20    >   IS_SMALLER                                       ~17     !2, 0
         21        BOOL                                             ~16     ~17
         22    > > JMPZ                                                     ~16, ->25
   34    23    >   ASSIGN                                                   !5, !3
   35    24        ASSIGN                                                   !2, !4
   13    25    > > JMP                                                      ->7
         26    >   FE_FREE                                                  $10
   39    27        ROPE_INIT                                     3  ~21     'Input+word%3A+'
         28        ROPE_ADD                                      1  ~21     ~21, !0
         29        ROPE_END                                      2  ~20     ~21, '%0A'
         30        ECHO                                                     ~20
   40    31        IS_EQUAL                                                 !2, 0
         32      > JMPZ                                                     ~23, ->38
   41    33    >   ROPE_INIT                                     3  ~25     'Exact+match+found%3A+'
         34        ROPE_ADD                                      1  ~25     ~25, !5
         35        ROPE_END                                      2  ~24     ~25, '%0A'
         36        ECHO                                                     ~24
         37      > JMP                                                      ->42
   43    38    >   ROPE_INIT                                     3  ~28     'Did+you+mean%3A+'
         39        ROPE_ADD                                      1  ~28     ~28, !5
         40        ROPE_END                                      2  ~27     ~28, '%3F%0A'
         41        ECHO                                                     ~27
   44    42    > > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.21 ms | 1400 KiB | 17 Q