3v4l.org

run code in 300+ PHP versions simultaneously
<?php $texts = [ "For example this is the text where I searched the keyword and I need the text around it too.", "keyword at the very start", "Or it can end with keyword", "Nothing to see here officer.", "keyword", ]; $needle = "keyword"; $extra = 10; foreach ($texts as $text) { $new = preg_replace_callback( "/.*?(\S+.{0,$extra})?($needle)(.{0,$extra}\S+)?.*/", function($m) { return sprintf( '%s<b>%s</b>%s', strlen($m[1]) ? "..{$m[1]}" : '', $m[2], strlen($m[3] ?? '') ? "{$m[3]}.." : '' ); }, $text, 1, $count ); echo ($count ? $new : '') . "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 28
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 28
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/bHEPQ
function name:  (null)
number of ops:  30
compiled vars:  !0 = $texts, !1 = $needle, !2 = $extra, !3 = $text, !4 = $new, !5 = $count
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   11     1        ASSIGN                                                   !1, 'keyword'
   12     2        ASSIGN                                                   !2, 10
   14     3      > FE_RESET_R                                       $9      !0, ->28
          4    > > FE_FETCH_R                                               $9, !3, ->28
   15     5    >   INIT_FCALL                                               'preg_replace_callback'
   16     6        ROPE_INIT                                     7  ~11     '%2F.%2A%3F%28%5CS%2B.%7B0%2C'
          7        ROPE_ADD                                      1  ~11     ~11, !2
          8        ROPE_ADD                                      2  ~11     ~11, '%7D%29%3F%28'
          9        ROPE_ADD                                      3  ~11     ~11, !1
         10        ROPE_ADD                                      4  ~11     ~11, '%29%28.%7B0%2C'
         11        ROPE_ADD                                      5  ~11     ~11, !2
         12        ROPE_END                                      6  ~10     ~11, '%7D%5CS%2B%29%3F.%2A%2F'
         13        SEND_VAL                                                 ~10
   17    14        DECLARE_LAMBDA_FUNCTION                          ~15     [0]
   24    15        SEND_VAL                                                 ~15
   25    16        SEND_VAR                                                 !3
   26    17        SEND_VAL                                                 1
   27    18        SEND_REF                                                 !5
   15    19        DO_ICALL                                         $16     
         20        ASSIGN                                                   !4, $16
   29    21      > JMPZ                                                     !5, ->24
         22    >   QM_ASSIGN                                        ~18     !4
         23      > JMP                                                      ->25
         24    >   QM_ASSIGN                                        ~18     ''
         25    >   CONCAT                                           ~19     ~18, '%0A'
         26        ECHO                                                     ~19
   14    27      > JMP                                                      ->4
         28    >   FE_FREE                                                  $9
   30    29      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 25
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 25
Branch analysis from position: 20
Branch analysis from position: 25
filename:       /in/bHEPQ
function name:  {closure}
number of ops:  30
compiled vars:  !0 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   18     1        INIT_FCALL                                               'sprintf'
   19     2        SEND_VAL                                                 '%25s%3Cb%3E%25s%3C%2Fb%3E%25s'
   20     3        FETCH_DIM_R                                      ~1      !0, 1
          4        STRLEN                                           ~2      ~1
          5      > JMPZ                                                     ~2, ->11
          6    >   NOP                                                      
          7        FETCH_DIM_R                                      ~3      !0, 1
          8        FAST_CONCAT                                      ~4      '..', ~3
          9        QM_ASSIGN                                        ~5      ~4
         10      > JMP                                                      ->12
         11    >   QM_ASSIGN                                        ~5      ''
         12    >   SEND_VAL                                                 ~5
   21    13        FETCH_DIM_R                                      ~6      !0, 2
         14        SEND_VAL                                                 ~6
   22    15        FETCH_DIM_IS                                     ~7      !0, 3
         16        COALESCE                                         ~8      ~7
         17        QM_ASSIGN                                        ~8      ''
         18        STRLEN                                           ~9      ~8
         19      > JMPZ                                                     ~9, ->25
         20    >   FETCH_DIM_R                                      ~10     !0, 3
         21        NOP                                                      
         22        FAST_CONCAT                                      ~11     ~10, '..'
         23        QM_ASSIGN                                        ~12     ~11
         24      > JMP                                                      ->26
         25    >   QM_ASSIGN                                        ~12     ''
         26    >   SEND_VAL                                                 ~12
   18    27        DO_ICALL                                         $13     
   22    28      > RETURN                                                   $13
   24    29*     > RETURN                                                   null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
136.44 ms | 1004 KiB | 15 Q