3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = "[vc_tab [vc_tab [vc_tab [vc_tab [vc_tab [vc_tab"; $active = 3; $needle = "[vc_tab"; $replace = '[vc_tab active="true"'; if( substr_count( $string, $needle) < $active) { throw new Exception("There aren't enough needles in the string to do this replacement"); } $count = 0; $index = 0; while( $count++ < $active) { $index = strpos( $string, $needle, $index) + strlen( $needle); } $result = substr( $string, $index - strlen( $needle)) . $replace . substr( $string, $index); echo "Index: ", $index, " ", $result;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 17
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 17
Branch analysis from position: 28
Branch analysis from position: 17
filename:       /in/OdbXj
function name:  (null)
number of ops:  46
compiled vars:  !0 = $string, !1 = $active, !2 = $needle, !3 = $replace, !4 = $count, !5 = $index, !6 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%5Bvc_tab+%5Bvc_tab+%5Bvc_tab+%5Bvc_tab+%5Bvc_tab+%5Bvc_tab'
    4     1        ASSIGN                                                   !1, 3
    5     2        ASSIGN                                                   !2, '%5Bvc_tab'
    6     3        ASSIGN                                                   !3, '%5Bvc_tab+active%3D%22true%22'
    8     4        INIT_FCALL                                               'substr_count'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !2
          7        DO_ICALL                                         $11     
          8        IS_SMALLER                                               $11, !1
          9      > JMPZ                                                     ~12, ->14
    9    10    >   NEW                                              $13     'Exception'
         11        SEND_VAL_EX                                              'There+aren%27t+enough+needles+in+the+string+to+do+this+replacement'
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $13
   12    14    >   ASSIGN                                                   !4, 0
         15        ASSIGN                                                   !5, 0
   13    16      > JMP                                                      ->25
   14    17    >   INIT_FCALL                                               'strpos'
         18        SEND_VAR                                                 !0
         19        SEND_VAR                                                 !2
         20        SEND_VAR                                                 !5
         21        DO_ICALL                                         $17     
         22        STRLEN                                           ~18     !2
         23        ADD                                              ~19     $17, ~18
         24        ASSIGN                                                   !5, ~19
   13    25    >   POST_INC                                         ~21     !4
         26        IS_SMALLER                                               ~21, !1
         27      > JMPNZ                                                    ~22, ->17
   17    28    >   INIT_FCALL                                               'substr'
         29        SEND_VAR                                                 !0
         30        STRLEN                                           ~23     !2
         31        SUB                                              ~24     !5, ~23
         32        SEND_VAL                                                 ~24
         33        DO_ICALL                                         $25     
         34        CONCAT                                           ~26     $25, !3
         35        INIT_FCALL                                               'substr'
         36        SEND_VAR                                                 !0
         37        SEND_VAR                                                 !5
         38        DO_ICALL                                         $27     
         39        CONCAT                                           ~28     ~26, $27
         40        ASSIGN                                                   !6, ~28
   18    41        ECHO                                                     'Index%3A+'
         42        ECHO                                                     !5
         43        ECHO                                                     '+'
         44        ECHO                                                     !6
         45      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.44 ms | 1400 KiB | 19 Q