3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * マルチバイト文字列、英数字の混じった文字列を1文字ずつ配列に分割 * 参考:http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1417635014# * mb_split, str_split, preg_splitことごとく使えない。 */ function mbStringToArray($string, $encoding = 'UTF-8') { $arrayResult = array(); while ($iLen = mb_strlen($string, $encoding)) { array_push($arrayResult, mb_substr($string, 0, 1, $encoding)); $string = mb_substr($string, 1, $iLen, $encoding); } return $arrayResult; } /** * 編集距離(レーベンシュタイン距離)を求める(マルチバイト文字対応) * @param $str1 * @param $str2 * @param $encoding * @param $costReplace * @return 数値(距離),かぶっていた文字の数 */ function LevenshteinDistance($str1, $str2, $costReplace = 2, $encoding = 'UTF-8') { $count_same_letter = 0; $d = array(); $mb_len1 = mb_strlen($str1, $encoding); $mb_len2 = mb_strlen($str2, $encoding); $mb_str1 = mbStringToArray($str1, $encoding); $mb_str2 = mbStringToArray($str2, $encoding); for ($i1 = 0; $i1 <= $mb_len1; $i1++) { $d[$i1] = array(); $d[$i1][0] = $i1; } for ($i2 = 0; $i2 <= $mb_len2; $i2++) { $d[0][$i2] = $i2; } for ($i1 = 1; $i1 <= $mb_len1; $i1++) { for ($i2 = 1; $i2 <= $mb_len2; $i2++) { // $cost = ($str1[$i1 - 1] == $str2[$i2 - 1]) ? 0 : 1; if ($mb_str1[$i1 - 1] === $mb_str2[$i2 - 1]) { $cost = 0; $count_same_letter++; } else { $cost = $costReplace; //置換 } $d[$i1][$i2] = min($d[$i1 - 1][$i2] + 1, //挿入 $d[$i1][$i2 - 1] + 1, //削除 $d[$i1 - 1][$i2 - 1] + $cost); } } return $d[$mb_len1][$mb_len2]; //return array('distance' => $d[$mb_len1][$mb_len2], 'count_same_letter' => $count_same_letter); } echo LevenshteinDistance("aaa","aax",1,"UTF-8"); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vKDkE
function name:  (null)
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   INIT_FCALL                                               'levenshteindistance'
          1        SEND_VAL                                                 'aaa'
          2        SEND_VAL                                                 'aax'
          3        SEND_VAL                                                 1
          4        SEND_VAL                                                 'UTF-8'
          5        DO_FCALL                                      0  $0      
          6        ECHO                                                     $0
   60     7      > RETURN                                                   1

Function mbstringtoarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 4
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 4
Branch analysis from position: 27
Branch analysis from position: 4
filename:       /in/vKDkE
function name:  mbStringToArray
number of ops:  29
compiled vars:  !0 = $string, !1 = $encoding, !2 = $arrayResult, !3 = $iLen
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'UTF-8'
    8     2        ASSIGN                                                   !2, <array>
    9     3      > JMP                                                      ->21
   10     4    >   INIT_FCALL                                               'array_push'
          5        SEND_REF                                                 !2
          6        INIT_FCALL                                               'mb_substr'
          7        SEND_VAR                                                 !0
          8        SEND_VAL                                                 0
          9        SEND_VAL                                                 1
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                         $5      
         12        SEND_VAR                                                 $5
         13        DO_ICALL                                                 
   11    14        INIT_FCALL                                               'mb_substr'
         15        SEND_VAR                                                 !0
         16        SEND_VAL                                                 1
         17        SEND_VAR                                                 !3
         18        SEND_VAR                                                 !1
         19        DO_ICALL                                         $7      
         20        ASSIGN                                                   !0, $7
    9    21    >   INIT_FCALL                                               'mb_strlen'
         22        SEND_VAR                                                 !0
         23        SEND_VAR                                                 !1
         24        DO_ICALL                                         $9      
         25        ASSIGN                                           ~10     !3, $9
         26      > JMPNZ                                                    ~10, ->4
   13    27    > > RETURN                                                   !2
   14    28*     > RETURN                                                   null

End of function mbstringtoarray

Function levenshteindistance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 28
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 38
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 83
Branch analysis from position: 83
2 jumps found. (Code = 44) Position 1 = 85, Position 2 = 46
Branch analysis from position: 85
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 48
Branch analysis from position: 82
2 jumps found. (Code = 44) Position 1 = 85, Position 2 = 46
Branch analysis from position: 85
Branch analysis from position: 46
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 54, Position 2 = 57
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 48
Branch analysis from position: 82
Branch analysis from position: 48
Branch analysis from position: 57
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 48
Branch analysis from position: 82
Branch analysis from position: 48
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 38
Branch analysis from position: 44
Branch analysis from position: 38
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 28
Branch analysis from position: 36
Branch analysis from position: 28
filename:       /in/vKDkE
function name:  LevenshteinDistance
number of ops:  89
compiled vars:  !0 = $str1, !1 = $str2, !2 = $costReplace, !3 = $encoding, !4 = $count_same_letter, !5 = $d, !6 = $mb_len1, !7 = $mb_len2, !8 = $mb_str1, !9 = $mb_str2, !10 = $i1, !11 = $i2, !12 = $cost
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      2
          3        RECV_INIT                                        !3      'UTF-8'
   25     4        ASSIGN                                                   !4, 0
   26     5        ASSIGN                                                   !5, <array>
   27     6        INIT_FCALL                                               'mb_strlen'
          7        SEND_VAR                                                 !0
          8        SEND_VAR                                                 !3
          9        DO_ICALL                                         $15     
         10        ASSIGN                                                   !6, $15
   28    11        INIT_FCALL                                               'mb_strlen'
         12        SEND_VAR                                                 !1
         13        SEND_VAR                                                 !3
         14        DO_ICALL                                         $17     
         15        ASSIGN                                                   !7, $17
   30    16        INIT_FCALL                                               'mbstringtoarray'
         17        SEND_VAR                                                 !0
         18        SEND_VAR                                                 !3
         19        DO_FCALL                                      0  $19     
         20        ASSIGN                                                   !8, $19
   31    21        INIT_FCALL                                               'mbstringtoarray'
         22        SEND_VAR                                                 !1
         23        SEND_VAR                                                 !3
         24        DO_FCALL                                      0  $21     
         25        ASSIGN                                                   !9, $21
   33    26        ASSIGN                                                   !10, 0
         27      > JMP                                                      ->34
   34    28    >   ASSIGN_DIM                                               !5, !10
         29        OP_DATA                                                  <array>
   35    30        FETCH_DIM_W                                      $25     !5, !10
         31        ASSIGN_DIM                                               $25, 0
         32        OP_DATA                                                  !10
   33    33        PRE_INC                                                  !10
         34    >   IS_SMALLER_OR_EQUAL                                      !10, !6
         35      > JMPNZ                                                    ~28, ->28
   38    36    >   ASSIGN                                                   !11, 0
         37      > JMP                                                      ->42
   39    38    >   FETCH_DIM_W                                      $30     !5, 0
         39        ASSIGN_DIM                                               $30, !11
         40        OP_DATA                                                  !11
   38    41        PRE_INC                                                  !11
         42    >   IS_SMALLER_OR_EQUAL                                      !11, !7
         43      > JMPNZ                                                    ~33, ->38
   42    44    >   ASSIGN                                                   !10, 1
         45      > JMP                                                      ->83
   43    46    >   ASSIGN                                                   !11, 1
         47      > JMP                                                      ->80
   45    48    >   SUB                                              ~36     !10, 1
         49        FETCH_DIM_R                                      ~37     !8, ~36
         50        SUB                                              ~38     !11, 1
         51        FETCH_DIM_R                                      ~39     !9, ~38
         52        IS_IDENTICAL                                             ~37, ~39
         53      > JMPZ                                                     ~40, ->57
   46    54    >   ASSIGN                                                   !12, 0
   47    55        PRE_INC                                                  !4
         56      > JMP                                                      ->58
   49    57    >   ASSIGN                                                   !12, !2
   51    58    >   INIT_FCALL                                               'min'
         59        SUB                                              ~46     !10, 1
         60        FETCH_DIM_R                                      ~47     !5, ~46
         61        FETCH_DIM_R                                      ~48     ~47, !11
         62        ADD                                              ~49     ~48, 1
         63        SEND_VAL                                                 ~49
   52    64        SUB                                              ~51     !11, 1
         65        FETCH_DIM_R                                      ~50     !5, !10
         66        FETCH_DIM_R                                      ~52     ~50, ~51
         67        ADD                                              ~53     ~52, 1
         68        SEND_VAL                                                 ~53
   53    69        SUB                                              ~54     !10, 1
         70        SUB                                              ~56     !11, 1
         71        FETCH_DIM_R                                      ~55     !5, ~54
         72        FETCH_DIM_R                                      ~57     ~55, ~56
         73        ADD                                              ~58     ~57, !12
         74        SEND_VAL                                                 ~58
         75        DO_ICALL                                         $59     
   51    76        FETCH_DIM_W                                      $44     !5, !10
         77        ASSIGN_DIM                                               $44, !11
   53    78        OP_DATA                                                  $59
   43    79        PRE_INC                                                  !11
         80    >   IS_SMALLER_OR_EQUAL                                      !11, !7
         81      > JMPNZ                                                    ~61, ->48
   42    82    >   PRE_INC                                                  !10
         83    >   IS_SMALLER_OR_EQUAL                                      !10, !6
         84      > JMPNZ                                                    ~63, ->46
   56    85    >   FETCH_DIM_R                                      ~64     !5, !6
         86        FETCH_DIM_R                                      ~65     ~64, !7
         87      > RETURN                                                   ~65
   58    88*     > RETURN                                                   null

End of function levenshteindistance

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.48 ms | 1410 KiB | 24 Q