3v4l.org

run code in 500+ PHP versions simultaneously
<?php $haystack = 'abdabc'; $needle = 'c'; $replace = 'z'; $limit = 2; function str_replace_limit($haystack, $needle, $replace, $limit, $start_pos = 0) { if ($limit < 1 || ($pos = strpos($haystack,$needle,$start_pos)) === false) { return $haystack; } else { return str_replace_limit( substr_replace($haystack, $replace, $pos, strlen($needle)), $needle, $replace, $limit - 1, $pos + strlen($replace) ); } } echo str_replace_limit($haystack, $needle, $replace, $limit);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Kdmqp
function name:  (null)
number of ops:  12
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $replace, !3 = $limit
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                       !0, 'abdabc'
    3     1        ASSIGN                                                       !1, 'c'
    4     2        ASSIGN                                                       !2, 'z'
    5     3        ASSIGN                                                       !3, 2
   21     4        INIT_FCALL                                                   'str_replace_limit'
          5        SEND_VAR                                                     !0
          6        SEND_VAR                                                     !1
          7        SEND_VAR                                                     !2
          8        SEND_VAR                                                     !3
          9        DO_FCALL                                          0  $8      
         10        ECHO                                                         $8
         11      > RETURN                                                       1

Function str_replace_limit:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/Kdmqp
function name:  str_replace_limit
number of ops:  34
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $replace, !3 = $limit, !4 = $start_pos, !5 = $pos
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    7     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
          3        RECV                                                 !3      
          4        RECV_INIT                                            !4      0
    8     5        IS_SMALLER                                           ~6      !3, 1
          6      > JMPNZ_EX                                             ~6      ~6, ->12
          7    >   FRAMELESS_ICALL_3                strpos              ~7      !0, !1
          8        OP_DATA                                                      !4
          9        ASSIGN                                               ~8      !5, ~7
         10        TYPE_CHECK                                        4  ~9      ~8
         11        BOOL                                                 ~6      ~9
         12    > > JMPZ                                                         ~6, ->15
    9    13    > > RETURN                                                       !0
    8    14*       JMP                                                          ->33
   11    15    >   INIT_FCALL_BY_NAME                                           'str_replace_limit'
   12    16        INIT_FCALL                                                   'substr_replace'
         17        SEND_VAR                                                     !0
         18        SEND_VAR                                                     !2
         19        SEND_VAR                                                     !5
         20        STRLEN                                               ~10     !1
         21        SEND_VAL                                                     ~10
         22        DO_ICALL                                             $11     
         23        SEND_VAR_NO_REF_EX                                           $11
         24        SEND_VAR_EX                                                  !1
         25        SEND_VAR_EX                                                  !2
   15    26        SUB                                                  ~12     !3, 1
         27        SEND_VAL_EX                                                  ~12
   16    28        STRLEN                                               ~13     !2
         29        ADD                                                  ~14     !5, ~13
         30        SEND_VAL_EX                                                  ~14
   11    31        DO_FCALL                                          0  $15     
   16    32      > RETURN                                                       $15
   19    33*     > RETURN                                                       null

End of function str_replace_limit

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
176.31 ms | 2284 KiB | 15 Q