3v4l.org

run code in 300+ PHP versions simultaneously
<?php function find_between($string, $start, $end, $trim = true, $greedy = false) { $pattern = '/' . preg_quote($start) . '(.*'; if (!$greedy) $pattern .= '?'; $pattern .= ')' . preg_quote($end) . '/'; preg_match($pattern, $string, $matches); $string = $matches[0]; if ($trim) { $string = substr($string, strlen($start)); $string = substr($string, 0, -strlen($start) + 1); } return $string; } function find_between2($string, $start, $end, $trim = true, $greedy = false) { $stringOut = ''; $pattern = '/'.preg_quote($start).'(.*'; if (!$greedy) $pattern .= '?'; $pattern .= ')'.preg_quote($end).'/'; preg_match($pattern, $string, $matches); if (count($matches)>1) { if ($trim) { $stringOut = $matches[1]; } else { $stringOut = $matches[0]; } } return ($stringOut==='')?false:$stringOut; } $string = 'The quick brown fox jumps over the lazy (not jumping) dog'; $start = 'quick'; $end = 'jump'; var_dump(find_between($string, $start, $end)); var_dump(find_between2($string, $start, $end));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FRkMQ
function name:  (null)
number of ops:  20
compiled vars:  !0 = $string, !1 = $start, !2 = $end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   ASSIGN                                                   !0, 'The+quick+brown+fox+jumps+over+the+lazy+%28not+jumping%29+dog'
   33     1        ASSIGN                                                   !1, 'quick'
   34     2        ASSIGN                                                   !2, 'jump'
   35     3        INIT_FCALL                                               'var_dump'
          4        INIT_FCALL                                               'find_between'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        SEND_VAR                                                 !2
          8        DO_FCALL                                      0  $6      
          9        SEND_VAR                                                 $6
         10        DO_ICALL                                                 
   36    11        INIT_FCALL                                               'var_dump'
         12        INIT_FCALL                                               'find_between2'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !1
         15        SEND_VAR                                                 !2
         16        DO_FCALL                                      0  $8      
         17        SEND_VAR                                                 $8
         18        DO_ICALL                                                 
         19      > RETURN                                                   1

Function find_between:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 14
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 43
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
Branch analysis from position: 14
filename:       /in/FRkMQ
function name:  find_between
number of ops:  45
compiled vars:  !0 = $string, !1 = $start, !2 = $end, !3 = $trim, !4 = $greedy, !5 = $pattern, !6 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      <true>
          4        RECV_INIT                                        !4      <false>
    4     5        INIT_FCALL                                               'preg_quote'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $7      
          8        CONCAT                                           ~8      '%2F', $7
          9        CONCAT                                           ~9      ~8, '%28.%2A'
         10        ASSIGN                                                   !5, ~9
    5    11        BOOL_NOT                                         ~11     !4
         12      > JMPZ                                                     ~11, ->14
         13    >   ASSIGN_OP                                     8          !5, '%3F'
    6    14    >   INIT_FCALL                                               'preg_quote'
         15        SEND_VAR                                                 !2
         16        DO_ICALL                                         $13     
         17        CONCAT                                           ~14     '%29', $13
         18        CONCAT                                           ~15     ~14, '%2F'
         19        ASSIGN_OP                                     8          !5, ~15
    7    20        INIT_FCALL                                               'preg_match'
         21        SEND_VAR                                                 !5
         22        SEND_VAR                                                 !0
         23        SEND_REF                                                 !6
         24        DO_ICALL                                                 
    8    25        FETCH_DIM_R                                      ~18     !6, 0
         26        ASSIGN                                                   !0, ~18
    9    27      > JMPZ                                                     !3, ->43
   10    28    >   INIT_FCALL                                               'substr'
         29        SEND_VAR                                                 !0
         30        STRLEN                                           ~20     !1
         31        SEND_VAL                                                 ~20
         32        DO_ICALL                                         $21     
         33        ASSIGN                                                   !0, $21
   11    34        INIT_FCALL                                               'substr'
         35        SEND_VAR                                                 !0
         36        SEND_VAL                                                 0
         37        STRLEN                                           ~23     !1
         38        MUL                                              ~24     ~23, -1
         39        ADD                                              ~25     ~24, 1
         40        SEND_VAL                                                 ~25
         41        DO_ICALL                                         $26     
         42        ASSIGN                                                   !0, $26
   13    43    > > RETURN                                                   !0
   14    44*     > RETURN                                                   null

End of function find_between

Function find_between2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 35
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 33
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 39
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 39
Branch analysis from position: 37
Branch analysis from position: 39
Branch analysis from position: 35
Branch analysis from position: 15
filename:       /in/FRkMQ
function name:  find_between2
number of ops:  42
compiled vars:  !0 = $string, !1 = $start, !2 = $end, !3 = $trim, !4 = $greedy, !5 = $stringOut, !6 = $pattern, !7 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      <true>
          4        RECV_INIT                                        !4      <false>
   17     5        ASSIGN                                                   !5, ''
   18     6        INIT_FCALL                                               'preg_quote'
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                         $9      
          9        CONCAT                                           ~10     '%2F', $9
         10        CONCAT                                           ~11     ~10, '%28.%2A'
         11        ASSIGN                                                   !6, ~11
   19    12        BOOL_NOT                                         ~13     !4
         13      > JMPZ                                                     ~13, ->15
         14    >   ASSIGN_OP                                     8          !6, '%3F'
   20    15    >   INIT_FCALL                                               'preg_quote'
         16        SEND_VAR                                                 !2
         17        DO_ICALL                                         $15     
         18        CONCAT                                           ~16     '%29', $15
         19        CONCAT                                           ~17     ~16, '%2F'
         20        ASSIGN_OP                                     8          !6, ~17
   21    21        INIT_FCALL                                               'preg_match'
         22        SEND_VAR                                                 !6
         23        SEND_VAR                                                 !0
         24        SEND_REF                                                 !7
         25        DO_ICALL                                                 
   22    26        COUNT                                            ~20     !7
         27        IS_SMALLER                                               1, ~20
         28      > JMPZ                                                     ~21, ->35
   23    29    > > JMPZ                                                     !3, ->33
   24    30    >   FETCH_DIM_R                                      ~22     !7, 1
         31        ASSIGN                                                   !5, ~22
         32      > JMP                                                      ->35
   26    33    >   FETCH_DIM_R                                      ~24     !7, 0
         34        ASSIGN                                                   !5, ~24
   29    35    >   IS_IDENTICAL                                             !5, ''
         36      > JMPZ                                                     ~26, ->39
         37    >   QM_ASSIGN                                        ~27     <false>
         38      > JMP                                                      ->40
         39    >   QM_ASSIGN                                        ~27     !5
         40    > > RETURN                                                   ~27
   30    41*     > RETURN                                                   null

End of function find_between2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.97 ms | 1407 KiB | 23 Q