3v4l.org

run code in 300+ PHP versions simultaneously
<?php $text = 'Here are @user:123 and @user:456 mentions'; $contextSize = 5; $pattern = '~ @user:[0-9]+ \s* (*:mention) | \S+ \s* ~x'; $results = []; if (preg_match_all($pattern, $text, $matches)) { foreach ($matches['MARK'] as $k => $v) { $beforeIndex = max(0, $k - $contextSize); $beforeSize = $k - $beforeIndex; $afterIndex = $k + 1; $results[] = [ 'before' => join(array_slice($matches[0], $beforeIndex, $beforeSize)), 'mention' => $matches[0][$k], 'after' => join(array_slice($matches[0], $afterIndex, $contextSize)) ]; } } print_r($results);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 51
Branch analysis from position: 10
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 50
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 50
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
Branch analysis from position: 51
filename:       /in/lPIhs
function name:  (null)
number of ops:  55
compiled vars:  !0 = $text, !1 = $contextSize, !2 = $pattern, !3 = $results, !4 = $matches, !5 = $v, !6 = $k, !7 = $beforeIndex, !8 = $beforeSize, !9 = $afterIndex
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'Here+are+%40user%3A123+and+%40user%3A456+mentions'
    5     1        ASSIGN                                                   !1, 5
    6     2        ASSIGN                                                   !2, '%7E+%40user%3A%5B0-9%5D%2B+%5Cs%2A+%28%2A%3Amention%29+%7C+%5CS%2B+%5Cs%2A+%7Ex'
    8     3        ASSIGN                                                   !3, <array>
   10     4        INIT_FCALL                                               'preg_match_all'
          5        SEND_VAR                                                 !2
          6        SEND_VAR                                                 !0
          7        SEND_REF                                                 !4
          8        DO_ICALL                                         $14     
          9      > JMPZ                                                     $14, ->51
   12    10    >   FETCH_DIM_R                                      ~15     !4, 'MARK'
         11      > FE_RESET_R                                       $16     ~15, ->50
         12    > > FE_FETCH_R                                       ~17     $16, !5, ->50
         13    >   ASSIGN                                                   !6, ~17
   13    14        INIT_FCALL                                               'max'
         15        SEND_VAL                                                 0
         16        SUB                                              ~19     !6, !1
         17        SEND_VAL                                                 ~19
         18        DO_ICALL                                         $20     
         19        ASSIGN                                                   !7, $20
   14    20        SUB                                              ~22     !6, !7
         21        ASSIGN                                                   !8, ~22
   15    22        ADD                                              ~24     !6, 1
         23        ASSIGN                                                   !9, ~24
   17    24        INIT_FCALL                                               'join'
         25        INIT_FCALL                                               'array_slice'
         26        FETCH_DIM_R                                      ~27     !4, 0
         27        SEND_VAL                                                 ~27
         28        SEND_VAR                                                 !7
         29        SEND_VAR                                                 !8
         30        DO_ICALL                                         $28     
         31        SEND_VAR                                                 $28
         32        DO_ICALL                                         $29     
         33        INIT_ARRAY                                       ~30     $29, 'before'
   18    34        FETCH_DIM_R                                      ~31     !4, 0
         35        FETCH_DIM_R                                      ~32     ~31, !6
         36        ADD_ARRAY_ELEMENT                                ~30     ~32, 'mention'
   19    37        INIT_FCALL                                               'join'
         38        INIT_FCALL                                               'array_slice'
         39        FETCH_DIM_R                                      ~33     !4, 0
         40        SEND_VAL                                                 ~33
         41        SEND_VAR                                                 !9
         42        SEND_VAR                                                 !1
         43        DO_ICALL                                         $34     
         44        SEND_VAR                                                 $34
         45        DO_ICALL                                         $35     
         46        ADD_ARRAY_ELEMENT                                ~30     $35, 'after'
   16    47        ASSIGN_DIM                                               !3
   19    48        OP_DATA                                                  ~30
   12    49      > JMP                                                      ->12
         50    >   FE_FREE                                                  $16
   24    51    >   INIT_FCALL                                               'print_r'
         52        SEND_VAR                                                 !3
         53        DO_ICALL                                                 
         54      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
186.4 ms | 1014 KiB | 18 Q