3v4l.org

run code in 300+ PHP versions simultaneously
<?php function mb_str_replace($search, $replace, $subject, $encoding = 'auto') { if(!is_array($search)) { $search = array($search); } if(!is_array($replace)) { $replace = array($replace); } if(strtolower($encoding) === 'auto') { $encoding = mb_internal_encoding(); } // $subject が複数ならば各要素に繰り返し適用する if(is_array($subject) || $subject instanceof Traversable) { $result = array(); foreach($subject as $key => $val) { $result[$key] = mb_str_replace($search, $replace, $val, $encoding); } return $result; } $currentpos = 0; // 現在の検索開始位置 while(true) { // $currentpos 以降で $search のいずれかが現れる位置を検索する $index = -1; // 見つけた文字列(最も前にあるもの)の $search の index $minpos = -1; // 見つけた文字列(最も前にあるもの)の位置 foreach($search as $key => $find) { if($find == '') { continue; } $findpos = mb_strpos($subject, $find, $currentpos, $encoding); if($findpos !== false) { if($minpos < 0 || $findpos < $minpos) { $minpos = $findpos; $index = $key; } } } // $search のいずれも見つからなければ終了 if($minpos < 0) { break; } // 置換実行 $r = array_key_exists($index, $replace) ? $replace[$index] : ''; $subject = mb_substr($subject, 0, $minpos, $encoding) . // 置換開始位置より前 $r . // 置換後文字列 mb_substr( // 置換終了位置より後ろ $subject, $minpos + mb_strlen($search[$index], $encoding), mb_strlen($subject, $encoding), $encoding); // 「現在位置」を $r の直後に設定 $currentpos = $minpos + mb_strlen($r, $encoding); } return $subject; } $test = 'this is {$tableName} test'; echo mb_str_replace('{$tableName}', 'timecard', $test);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h8i7Q
function name:  (null)
number of ops:  8
compiled vars:  !0 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   ASSIGN                                                   !0, 'this+is+%7B%24tableName%7D+test'
   64     1        INIT_FCALL                                               'mb_str_replace'
          2        SEND_VAL                                                 '%7B%24tableName%7D'
          3        SEND_VAL                                                 'timecard'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $2      
          6        ECHO                                                     $2
          7      > RETURN                                                   1

Function mb_str_replace:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 22
Branch analysis from position: 19
2 jumps found. (Code = 47) Position 1 = 24, Position 2 = 26
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 42
Branch analysis from position: 27
2 jumps found. (Code = 77) Position 1 = 29, Position 2 = 40
Branch analysis from position: 29
2 jumps found. (Code = 78) Position 1 = 30, Position 2 = 40
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 111
Branch analysis from position: 111
2 jumps found. (Code = 44) Position 1 = 112, Position 2 = 44
Branch analysis from position: 112
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
2 jumps found. (Code = 77) Position 1 = 47, Position 2 = 69
Branch analysis from position: 47
2 jumps found. (Code = 78) Position 1 = 48, Position 2 = 69
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 52
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 52
2 jumps found. (Code = 43) Position 1 = 61, Position 2 = 68
Branch analysis from position: 61
2 jumps found. (Code = 47) Position 1 = 63, Position 2 = 65
Branch analysis from position: 63
2 jumps found. (Code = 43) Position 1 = 66, Position 2 = 68
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 68
Branch analysis from position: 65
Branch analysis from position: 68
Branch analysis from position: 69
2 jumps found. (Code = 43) Position 1 = 72, Position 2 = 73
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 112
Branch analysis from position: 112
Branch analysis from position: 73
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 78
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 79
Branch analysis from position: 79
2 jumps found. (Code = 44) Position 1 = 112, Position 2 = 44
Branch analysis from position: 112
Branch analysis from position: 44
Branch analysis from position: 78
2 jumps found. (Code = 44) Position 1 = 112, Position 2 = 44
Branch analysis from position: 112
Branch analysis from position: 44
Branch analysis from position: 69
Branch analysis from position: 26
Branch analysis from position: 22
Branch analysis from position: 14
Branch analysis from position: 9
filename:       /in/h8i7Q
function name:  mb_str_replace
number of ops:  114
compiled vars:  !0 = $search, !1 = $replace, !2 = $subject, !3 = $encoding, !4 = $result, !5 = $val, !6 = $key, !7 = $currentpos, !8 = $index, !9 = $minpos, !10 = $find, !11 = $findpos, !12 = $r
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      'auto'
    3     4        TYPE_CHECK                                  128  ~13     !0
          5        BOOL_NOT                                         ~14     ~13
          6      > JMPZ                                                     ~14, ->9
    4     7    >   INIT_ARRAY                                       ~15     !0
          8        ASSIGN                                                   !0, ~15
    6     9    >   TYPE_CHECK                                  128  ~17     !1
         10        BOOL_NOT                                         ~18     ~17
         11      > JMPZ                                                     ~18, ->14
    7    12    >   INIT_ARRAY                                       ~19     !1
         13        ASSIGN                                                   !1, ~19
    9    14    >   INIT_FCALL                                               'strtolower'
         15        SEND_VAR                                                 !3
         16        DO_ICALL                                         $21     
         17        IS_IDENTICAL                                             $21, 'auto'
         18      > JMPZ                                                     ~22, ->22
   10    19    >   INIT_FCALL                                               'mb_internal_encoding'
         20        DO_ICALL                                         $23     
         21        ASSIGN                                                   !3, $23
   14    22    >   TYPE_CHECK                                  128  ~25     !2
         23      > JMPNZ_EX                                         ~25     ~25, ->26
         24    >   INSTANCEOF                                       ~26     !2, 'Traversable'
         25        BOOL                                             ~25     ~26
         26    > > JMPZ                                                     ~25, ->42
   15    27    >   ASSIGN                                                   !4, <array>
   16    28      > FE_RESET_R                                       $28     !2, ->40
         29    > > FE_FETCH_R                                       ~29     $28, !5, ->40
         30    >   ASSIGN                                                   !6, ~29
   17    31        INIT_FCALL_BY_NAME                                       'mb_str_replace'
         32        SEND_VAR_EX                                              !0
         33        SEND_VAR_EX                                              !1
         34        SEND_VAR_EX                                              !5
         35        SEND_VAR_EX                                              !3
         36        DO_FCALL                                      0  $32     
         37        ASSIGN_DIM                                               !4, !6
         38        OP_DATA                                                  $32
   16    39      > JMP                                                      ->29
         40    >   FE_FREE                                                  $28
   19    41      > RETURN                                                   !4
   22    42    >   ASSIGN                                                   !7, 0
   23    43      > JMP                                                      ->111
   25    44    >   ASSIGN                                                   !8, -1
   26    45        ASSIGN                                                   !9, -1
   27    46      > FE_RESET_R                                       $36     !0, ->69
         47    > > FE_FETCH_R                                       ~37     $36, !10, ->69
         48    >   ASSIGN                                                   !6, ~37
   28    49        IS_EQUAL                                                 !10, ''
         50      > JMPZ                                                     ~39, ->52
   29    51    > > JMP                                                      ->47
   31    52    >   INIT_FCALL                                               'mb_strpos'
         53        SEND_VAR                                                 !2
         54        SEND_VAR                                                 !10
         55        SEND_VAR                                                 !7
         56        SEND_VAR                                                 !3
         57        DO_ICALL                                         $40     
         58        ASSIGN                                                   !11, $40
   32    59        TYPE_CHECK                                  1018          !11
         60      > JMPZ                                                     ~42, ->68
   33    61    >   IS_SMALLER                                       ~43     !9, 0
         62      > JMPNZ_EX                                         ~43     ~43, ->65
         63    >   IS_SMALLER                                       ~44     !11, !9
         64        BOOL                                             ~43     ~44
         65    > > JMPZ                                                     ~43, ->68
   34    66    >   ASSIGN                                                   !9, !11
   35    67        ASSIGN                                                   !8, !6
   27    68    > > JMP                                                      ->47
         69    >   FE_FREE                                                  $36
   41    70        IS_SMALLER                                               !9, 0
         71      > JMPZ                                                     ~47, ->73
   42    72    > > JMP                                                      ->112
   46    73    >   ARRAY_KEY_EXISTS                                         !8, !1
         74      > JMPZ                                                     ~48, ->78
         75    >   FETCH_DIM_R                                      ~49     !1, !8
         76        QM_ASSIGN                                        ~50     ~49
         77      > JMP                                                      ->79
         78    >   QM_ASSIGN                                        ~50     ''
         79    >   ASSIGN                                                   !12, ~50
   48    80        INIT_FCALL                                               'mb_substr'
         81        SEND_VAR                                                 !2
         82        SEND_VAL                                                 0
         83        SEND_VAR                                                 !9
         84        SEND_VAR                                                 !3
         85        DO_ICALL                                         $52     
   49    86        CONCAT                                           ~53     $52, !12
   50    87        INIT_FCALL                                               'mb_substr'
   51    88        SEND_VAR                                                 !2
   52    89        INIT_FCALL                                               'mb_strlen'
         90        FETCH_DIM_R                                      ~54     !0, !8
         91        SEND_VAL                                                 ~54
         92        SEND_VAR                                                 !3
         93        DO_ICALL                                         $55     
         94        ADD                                              ~56     !9, $55
         95        SEND_VAL                                                 ~56
   53    96        INIT_FCALL                                               'mb_strlen'
         97        SEND_VAR                                                 !2
         98        SEND_VAR                                                 !3
         99        DO_ICALL                                         $57     
        100        SEND_VAR                                                 $57
   54   101        SEND_VAR                                                 !3
        102        DO_ICALL                                         $58     
        103        CONCAT                                           ~59     ~53, $58
   47   104        ASSIGN                                                   !2, ~59
   57   105        INIT_FCALL                                               'mb_strlen'
        106        SEND_VAR                                                 !12
        107        SEND_VAR                                                 !3
        108        DO_ICALL                                         $61     
        109        ADD                                              ~62     !9, $61
        110        ASSIGN                                                   !7, ~62
   23   111    > > JMPNZ                                                    <true>, ->44
   59   112    > > RETURN                                                   !2
   60   113*     > RETURN                                                   null

End of function mb_str_replace

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
272.81 ms | 1415 KiB | 25 Q