3v4l.org

run code in 500+ 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 = 62
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 67, Position 2 = 14
Branch analysis from position: 67
2 jumps found. (Code = 43) Position 1 = 74, Position 2 = 78
Branch analysis from position: 74
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 78
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 23
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 45
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 67, Position 2 = 14
Branch analysis from position: 67
Branch analysis from position: 14
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 58
Branch analysis from position: 50
2 jumps found. (Code = 44) Position 1 = 67, Position 2 = 14
Branch analysis from position: 67
Branch analysis from position: 14
Branch analysis from position: 58
filename:       /in/TQLmn
function name:  getDelimitedStrings
number of ops:  80
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                                                          ->62
   25    14    >   ASSIGN_OP                                         1          !7, !4
   26    15        FRAMELESS_ICALL_3                strpos              ~24     !0, !2
         16        OP_DATA                                                      !7
         17        ASSIGN                                                       !8, ~24
   27    18        SUB                                                  ~26     !7, 1
         19        ASSIGN                                                       !10, ~26
   28    20        TYPE_CHECK                                        4          !8
         21      > JMPZ                                                         ~28, ->23
   29    22    > > JMP                                                          ->67
   31    23    >   SUB                                                  ~31     !7, !4
         24        MUL                                                  ~32     !4, 2
         25        ADD                                                  ~33     !8, ~32
         26        ADD                                                  ~34     ~33, 1
         27        SUB                                                  ~35     ~34, !7
         28        FRAMELESS_ICALL_3                substr              ~36     !0, ~31
         29        OP_DATA                                                      ~35
         30        FETCH_DIM_W                                          $29     !3, 'in'
         31        ASSIGN_DIM                                                   $29
         32        OP_DATA                                                      ~36
   32    33      > JMPZ                                                         !9, ->45
   33    34    >   ADD                                                  ~39     !9, !4
         35        ADD                                                  ~40     ~39, 1
         36        SUB                                                  ~41     !10, !9
         37        MUL                                                  ~42     !4, 2
         38        SUB                                                  ~43     ~41, ~42
         39        FRAMELESS_ICALL_3                substr              ~44     !0, ~40
         40        OP_DATA                                                      ~43
         41        FETCH_DIM_W                                          $37     !3, 'out'
         42        ASSIGN_DIM                                                   $37
         43        OP_DATA                                                      ~44
   32    44      > JMP                                                          ->58
   34    45    >   SUB                                                  ~45     !10, !9
         46        SUB                                                  ~46     !4, 1
         47        SUB                                                  ~47     ~45, ~46
         48        IS_SMALLER                                                   0, ~47
         49      > JMPZ                                                         ~48, ->58
   35    50    >   SUB                                                  ~51     !10, !9
         51        SUB                                                  ~52     !4, 1
         52        SUB                                                  ~53     ~51, ~52
         53        FRAMELESS_ICALL_3                substr              ~54     !0, !9
         54        OP_DATA                                                      ~53
         55        FETCH_DIM_W                                          $49     !3, 'out'
         56        ASSIGN_DIM                                                   $49
         57        OP_DATA                                                      ~54
   37    58    >   ADD                                                  ~55     !8, !5
         59        ASSIGN                                                       !6, ~55
   38    60        ASSIGN                                                       !6, !8
   39    61        ASSIGN                                                       !9, !6
   24    62    >   FRAMELESS_ICALL_3                strpos              ~59     !0, !1
         63        OP_DATA                                                      !6
         64        ASSIGN                                               ~60     !7, ~59
         65        TYPE_CHECK                                      1018          ~60
         66      > JMPNZ                                                        ~61, ->14
   41    67    >   STRLEN                                               ~62     !0
         68        ASSIGN                                                       !11, ~62
   42    69        ADD                                                  ~64     !9, !4
         70        ADD                                                  ~65     ~64, 1
         71        ASSIGN                                                       !12, ~65
   43    72        IS_SMALLER                                                   !12, !11
         73      > JMPZ                                                         ~67, ->78
   44    74    >   FRAMELESS_ICALL_2                substr              ~70     !0, !12
         75        FETCH_DIM_W                                          $68     !3, 'out'
         76        ASSIGN_DIM                                                   $68
         77        OP_DATA                                                      ~70
   46    78    > > RETURN                                                       !3
   47    79*     > RETURN                                                       null

End of function getdelimitedstrings

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
166.46 ms | 2362 KiB | 18 Q