3v4l.org

run code in 300+ PHP versions simultaneously
<?php $test_strings = [ '0<p>a</p>1<p>b</p>2<p>c</p>3', '0<p>a</p>1<p>b</p>2<p>c</p>', '<p>a</p>1<p>b</p>2<p>c</p>3', '<p>a</p>1<p>b</p>2<p>c</p>', '<p></p>1<p>b' ]; /** * Seperate a block of code by sub blocks. Example, removing all <script>...<script> tags from HTML kode * * @param string $str, text block * @param string $startDelimiter, string to match for start of block to be extracted * @param string $endDelimiter, string to match for ending the block to be extracted * @return array [all full blocks, whats left of string] */ function getDelimitedStrings($str, $startDelimiter, $endDelimiter) { $contents = array(); $startDelimiterLength = strlen($startDelimiter); $endDelimiterLength = strlen($endDelimiter); $startFrom = $contentStart = $contentEnd = $outStart = $outEnd = 0; while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) { $contentStart += $startDelimiterLength; $contentEnd = strpos($str, $endDelimiter, $contentStart); $outEnd = $contentStart - 1; if (false === $contentEnd) { break; } $contents['in'][] = substr($str, ($contentStart-$startDelimiterLength), ($contentEnd + ($startDelimiterLength*2) +1) - $contentStart); if( $outStart ){ $contents['out'][] = substr($str, ($outStart+$startDelimiterLength+1), $outEnd - $outStart - ($startDelimiterLength*2)); } else if( ($outEnd - $outStart - ($startDelimiterLength-1)) > 0 ){ $contents['out'][] = substr($str, $outStart, $outEnd - $outStart - ($startDelimiterLength-1)); } $startFrom = $contentEnd + $endDelimiterLength; $startFrom = $contentEnd; $outStart = $startFrom; } $total_length = strlen($str); $current_position = $outStart + $startDelimiterLength + 1; if( $current_position < $total_length ) $contents['out'][] = substr($str, $current_position); return $contents; } foreach($test_strings AS $string){ var_dump( getDelimitedStrings($string, '<p>', '</p>') ); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 12
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/TQLmn
function name:  (null)
number of ops:  14
compiled vars:  !0 = $test_strings, !1 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   49     1      > FE_RESET_R                                       $3      !0, ->12
          2    > > FE_FETCH_R                                               $3, !1, ->12
   50     3    >   INIT_FCALL                                               'var_dump'
          4        INIT_FCALL                                               'getdelimitedstrings'
          5        SEND_VAR                                                 !1
          6        SEND_VAL                                                 '%3Cp%3E'
          7        SEND_VAL                                                 '%3C%2Fp%3E'
          8        DO_FCALL                                      0  $4      
          9        SEND_VAR                                                 $4
         10        DO_ICALL                                                 
   49    11      > JMP                                                      ->2
         12    >   FE_FREE                                                  $3
   51    13      > RETURN                                                   1

Function getdelimitedstrings:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 74
Branch analysis from position: 74
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 14
Branch analysis from position: 82
2 jumps found. (Code = 43) Position 1 = 89, Position 2 = 96
Branch analysis from position: 89
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 96
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 26
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 82
Branch analysis from position: 82
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 54
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 14
Branch analysis from position: 82
Branch analysis from position: 14
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 70
Branch analysis from position: 59
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 14
Branch analysis from position: 82
Branch analysis from position: 14
Branch analysis from position: 70
filename:       /in/TQLmn
function name:  getDelimitedStrings
number of ops:  98
compiled vars:  !0 = $str, !1 = $startDelimiter, !2 = $endDelimiter, !3 = $contents, !4 = $startDelimiterLength, !5 = $endDelimiterLength, !6 = $startFrom, !7 = $contentStart, !8 = $contentEnd, !9 = $outStart, !10 = $outEnd, !11 = $total_length, !12 = $current_position
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   20     3        ASSIGN                                                   !3, <array>
   21     4        STRLEN                                           ~14     !1
          5        ASSIGN                                                   !4, ~14
   22     6        STRLEN                                           ~16     !2
          7        ASSIGN                                                   !5, ~16
   23     8        ASSIGN                                           ~18     !10, 0
          9        ASSIGN                                           ~19     !9, ~18
         10        ASSIGN                                           ~20     !8, ~19
         11        ASSIGN                                           ~21     !7, ~20
         12        ASSIGN                                                   !6, ~21
   24    13      > JMP                                                      ->74
   25    14    >   ASSIGN_OP                                     1          !7, !4
   26    15        INIT_FCALL                                               'strpos'
         16        SEND_VAR                                                 !0
         17        SEND_VAR                                                 !2
         18        SEND_VAR                                                 !7
         19        DO_ICALL                                         $24     
         20        ASSIGN                                                   !8, $24
   27    21        SUB                                              ~26     !7, 1
         22        ASSIGN                                                   !10, ~26
   28    23        TYPE_CHECK                                    4          !8
         24      > JMPZ                                                     ~28, ->26
   29    25    > > JMP                                                      ->82
   31    26    >   INIT_FCALL                                               'substr'
         27        SEND_VAR                                                 !0
         28        SUB                                              ~31     !7, !4
         29        SEND_VAL                                                 ~31
         30        MUL                                              ~32     !4, 2
         31        ADD                                              ~33     !8, ~32
         32        ADD                                              ~34     ~33, 1
         33        SUB                                              ~35     ~34, !7
         34        SEND_VAL                                                 ~35
         35        DO_ICALL                                         $36     
         36        FETCH_DIM_W                                      $29     !3, 'in'
         37        ASSIGN_DIM                                               $29
         38        OP_DATA                                                  $36
   32    39      > JMPZ                                                     !9, ->54
   33    40    >   INIT_FCALL                                               'substr'
         41        SEND_VAR                                                 !0
         42        ADD                                              ~39     !9, !4
         43        ADD                                              ~40     ~39, 1
         44        SEND_VAL                                                 ~40
         45        SUB                                              ~41     !10, !9
         46        MUL                                              ~42     !4, 2
         47        SUB                                              ~43     ~41, ~42
         48        SEND_VAL                                                 ~43
         49        DO_ICALL                                         $44     
         50        FETCH_DIM_W                                      $37     !3, 'out'
         51        ASSIGN_DIM                                               $37
         52        OP_DATA                                                  $44
         53      > JMP                                                      ->70
   34    54    >   SUB                                              ~45     !10, !9
         55        SUB                                              ~46     !4, 1
         56        SUB                                              ~47     ~45, ~46
         57        IS_SMALLER                                               0, ~47
         58      > JMPZ                                                     ~48, ->70
   35    59    >   INIT_FCALL                                               'substr'
         60        SEND_VAR                                                 !0
         61        SEND_VAR                                                 !9
         62        SUB                                              ~51     !10, !9
         63        SUB                                              ~52     !4, 1
         64        SUB                                              ~53     ~51, ~52
         65        SEND_VAL                                                 ~53
         66        DO_ICALL                                         $54     
         67        FETCH_DIM_W                                      $49     !3, 'out'
         68        ASSIGN_DIM                                               $49
         69        OP_DATA                                                  $54
   37    70    >   ADD                                              ~55     !8, !5
         71        ASSIGN                                                   !6, ~55
   38    72        ASSIGN                                                   !6, !8
   39    73        ASSIGN                                                   !9, !6
   24    74    >   INIT_FCALL                                               'strpos'
         75        SEND_VAR                                                 !0
         76        SEND_VAR                                                 !1
         77        SEND_VAR                                                 !6
         78        DO_ICALL                                         $59     
         79        ASSIGN                                           ~60     !7, $59
         80        TYPE_CHECK                                  1018          ~60
         81      > JMPNZ                                                    ~61, ->14
   41    82    >   STRLEN                                           ~62     !0
         83        ASSIGN                                                   !11, ~62
   42    84        ADD                                              ~64     !9, !4
         85        ADD                                              ~65     ~64, 1
         86        ASSIGN                                                   !12, ~65
   43    87        IS_SMALLER                                               !12, !11
         88      > JMPZ                                                     ~67, ->96
   44    89    >   INIT_FCALL                                               'substr'
         90        SEND_VAR                                                 !0
         91        SEND_VAR                                                 !12
         92        DO_ICALL                                         $70     
         93        FETCH_DIM_W                                      $68     !3, 'out'
         94        ASSIGN_DIM                                               $68
         95        OP_DATA                                                  $70
   46    96    > > RETURN                                                   !3
   47    97*     > RETURN                                                   null

End of function getdelimitedstrings

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.69 ms | 1417 KiB | 20 Q