3v4l.org

run code in 300+ PHP versions simultaneously
<?php // input misspelled word $input = 'carrot'; // array of words to check against $words = array('apple','pineapple','banana','orange', 'radish','carrot','pea','bean','potato'); // no shortest distance found, yet $shortest = -1; // 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 $lev."\n"; echo "Exact match found: $closest\n"; } else { echo $lev."\n"; echo "Did you mean: $closest?\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 23
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 23
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 37
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 47) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 22
Branch analysis from position: 19
Branch analysis from position: 23
Branch analysis from position: 23
filename:       /in/aDrkg
function name:  (null)
number of ops:  44
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, 'carrot'
    6     1        ASSIGN                                                   !1, <array>
   10     2        ASSIGN                                                   !2, -1
   13     3      > FE_RESET_R                                       $9      !1, ->23
          4    > > FE_FETCH_R                                               $9, !3, ->23
   17     5    >   INIT_FCALL                                               'levenshtein'
          6        SEND_VAR                                                 !0
          7        SEND_VAR                                                 !3
          8        DO_ICALL                                         $10     
          9        ASSIGN                                                   !4, $10
   20    10        IS_EQUAL                                                 !4, 0
         11      > JMPZ                                                     ~12, ->15
   23    12    >   ASSIGN                                                   !5, !3
   24    13        ASSIGN                                                   !2, 0
   27    14      > JMP                                                      ->23
   32    15    >   IS_SMALLER_OR_EQUAL                              ~15     !4, !2
         16      > JMPNZ_EX                                         ~15     ~15, ->19
         17    >   IS_SMALLER                                       ~16     !2, 0
         18        BOOL                                             ~15     ~16
         19    > > JMPZ                                                     ~15, ->22
   34    20    >   ASSIGN                                                   !5, !3
   35    21        ASSIGN                                                   !2, !4
   13    22    > > JMP                                                      ->4
         23    >   FE_FREE                                                  $9
   39    24        ROPE_INIT                                     3  ~20     'Input+word%3A+'
         25        ROPE_ADD                                      1  ~20     ~20, !0
         26        ROPE_END                                      2  ~19     ~20, '%0A'
         27        ECHO                                                     ~19
   40    28        IS_EQUAL                                                 !2, 0
         29      > JMPZ                                                     ~22, ->37
   41    30    >   CONCAT                                           ~23     !4, '%0A'
         31        ECHO                                                     ~23
   42    32        ROPE_INIT                                     3  ~25     'Exact+match+found%3A+'
         33        ROPE_ADD                                      1  ~25     ~25, !5
         34        ROPE_END                                      2  ~24     ~25, '%0A'
         35        ECHO                                                     ~24
         36      > JMP                                                      ->43
   44    37    >   CONCAT                                           ~27     !4, '%0A'
         38        ECHO                                                     ~27
   45    39        ROPE_INIT                                     3  ~29     'Did+you+mean%3A+'
         40        ROPE_ADD                                      1  ~29     ~29, !5
         41        ROPE_END                                      2  ~28     ~29, '%3F%0A'
         42        ECHO                                                     ~28
   46    43    > > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.97 ms | 1400 KiB | 15 Q