3v4l.org

run code in 300+ PHP versions simultaneously
<?php function cutMessage($text, $search) { $pieces = explode(' ', $text); $firstWord = $pieces[0]; $lastWord = array_pop($pieces); $pattern = "/((?:\w+\W+){0,5})" . preg_quote($search, "/") . "((?:\W+\w+){0,5})([!.]?)/"; preg_match($pattern, $text, $matches); $returnText = ''; $pieces = explode(' ', $matches[1]); if (!empty($matches[1]) && $pieces[0] != $firstWord) { $returnText .= '... ' . $matches[1]; } elseif (!empty($matches[1])) { $returnText .= $matches[1]; } $returnText .= $search; $pieces = explode(' ', $matches[2]); if (!empty($matches[3])) { $returnText .= $matches[2] . $matches[3]; } elseif (!empty($matches[2]) && array_pop($pieces) != $lastWord) { $returnText .= $matches[2] . ' ...'; } elseif (!empty($matches[2])) { $returnText .= $matches[2]; } return $returnText; } $texts = [ "Lorem ipsum dolor sit amet, consectetur {query:ABCD:1234} adipiscing elit. Mauris consequat, quam id feugiat varius.", "{query:ABCD:1234} Lorem ipsum dolor sit amet!", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris consequat, quam id feugiat varius {query:ABCD:1234}.", "Lorem ipsum dolor sit amet {query:ABCD:1234} adipiscing elit. Mauris consequat, quam id feugiat varius.", ]; foreach ($texts as $text) { $res = cutMessage($text, "{query:ABCD:1234}"); echo $res . PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 11
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/Un8gf
function name:  (null)
number of ops:  13
compiled vars:  !0 = $texts, !1 = $text, !2 = $res
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   ASSIGN                                                   !0, <array>
   42     1      > FE_RESET_R                                       $4      !0, ->11
          2    > > FE_FETCH_R                                               $4, !1, ->11
   43     3    >   INIT_FCALL                                               'cutmessage'
          4        SEND_VAR                                                 !1
          5        SEND_VAL                                                 '%7Bquery%3AABCD%3A1234%7D'
          6        DO_FCALL                                      0  $5      
          7        ASSIGN                                                   !2, $5
   44     8        CONCAT                                           ~7      !2, '%0A'
          9        ECHO                                                     ~7
   42    10      > JMP                                                      ->2
         11    >   FE_FREE                                                  $4
   45    12      > RETURN                                                   1

Function cutmessage:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 35, Position 2 = 38
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 43
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 63
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 63
2 jumps found. (Code = 46) Position 1 = 66, Position 2 = 71
Branch analysis from position: 66
2 jumps found. (Code = 43) Position 1 = 72, Position 2 = 76
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
Branch analysis from position: 76
2 jumps found. (Code = 43) Position 1 = 79, Position 2 = 81
Branch analysis from position: 79
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 81
Branch analysis from position: 71
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 48
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 63
Branch analysis from position: 58
Branch analysis from position: 63
Branch analysis from position: 48
Branch analysis from position: 38
filename:       /in/Un8gf
function name:  cutMessage
number of ops:  83
compiled vars:  !0 = $text, !1 = $search, !2 = $pieces, !3 = $firstWord, !4 = $lastWord, !5 = $pattern, !6 = $matches, !7 = $returnText
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '+'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $8      
          6        ASSIGN                                                   !2, $8
    6     7        FETCH_DIM_R                                      ~10     !2, 0
          8        ASSIGN                                                   !3, ~10
    7     9        INIT_FCALL                                               'array_pop'
         10        SEND_REF                                                 !2
         11        DO_ICALL                                         $12     
         12        ASSIGN                                                   !4, $12
    9    13        INIT_FCALL                                               'preg_quote'
         14        SEND_VAR                                                 !1
         15        SEND_VAL                                                 '%2F'
         16        DO_ICALL                                         $14     
         17        CONCAT                                           ~15     '%2F%28%28%3F%3A%5Cw%2B%5CW%2B%29%7B0%2C5%7D%29', $14
         18        CONCAT                                           ~16     ~15, '%28%28%3F%3A%5CW%2B%5Cw%2B%29%7B0%2C5%7D%29%28%5B%21.%5D%3F%29%2F'
         19        ASSIGN                                                   !5, ~16
   10    20        INIT_FCALL                                               'preg_match'
         21        SEND_VAR                                                 !5
         22        SEND_VAR                                                 !0
         23        SEND_REF                                                 !6
         24        DO_ICALL                                                 
   12    25        ASSIGN                                                   !7, ''
   14    26        INIT_FCALL                                               'explode'
         27        SEND_VAL                                                 '+'
         28        FETCH_DIM_R                                      ~20     !6, 1
         29        SEND_VAL                                                 ~20
         30        DO_ICALL                                         $21     
         31        ASSIGN                                                   !2, $21
   15    32        ISSET_ISEMPTY_DIM_OBJ                         1  ~23     !6, 1
         33        BOOL_NOT                                         ~24     ~23
         34      > JMPZ_EX                                          ~24     ~24, ->38
         35    >   FETCH_DIM_R                                      ~25     !2, 0
         36        IS_NOT_EQUAL                                     ~26     !3, ~25
         37        BOOL                                             ~24     ~26
         38    > > JMPZ                                                     ~24, ->43
   16    39    >   FETCH_DIM_R                                      ~27     !6, 1
         40        CONCAT                                           ~28     '...+', ~27
         41        ASSIGN_OP                                     8          !7, ~28
         42      > JMP                                                      ->48
   17    43    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~30     !6, 1
         44        BOOL_NOT                                         ~31     ~30
         45      > JMPZ                                                     ~31, ->48
   18    46    >   FETCH_DIM_R                                      ~32     !6, 1
         47        ASSIGN_OP                                     8          !7, ~32
   21    48    >   ASSIGN_OP                                     8          !7, !1
   23    49        INIT_FCALL                                               'explode'
         50        SEND_VAL                                                 '+'
         51        FETCH_DIM_R                                      ~35     !6, 2
         52        SEND_VAL                                                 ~35
         53        DO_ICALL                                         $36     
         54        ASSIGN                                                   !2, $36
   25    55        ISSET_ISEMPTY_DIM_OBJ                         1  ~38     !6, 3
         56        BOOL_NOT                                         ~39     ~38
         57      > JMPZ                                                     ~39, ->63
   26    58    >   FETCH_DIM_R                                      ~40     !6, 2
         59        FETCH_DIM_R                                      ~41     !6, 3
         60        CONCAT                                           ~42     ~40, ~41
         61        ASSIGN_OP                                     8          !7, ~42
         62      > JMP                                                      ->81
   27    63    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~44     !6, 2
         64        BOOL_NOT                                         ~45     ~44
         65      > JMPZ_EX                                          ~45     ~45, ->71
         66    >   INIT_FCALL                                               'array_pop'
         67        SEND_REF                                                 !2
         68        DO_ICALL                                         $46     
         69        IS_NOT_EQUAL                                     ~47     !4, $46
         70        BOOL                                             ~45     ~47
         71    > > JMPZ                                                     ~45, ->76
   28    72    >   FETCH_DIM_R                                      ~48     !6, 2
         73        CONCAT                                           ~49     ~48, '+...'
         74        ASSIGN_OP                                     8          !7, ~49
         75      > JMP                                                      ->81
   29    76    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~51     !6, 2
         77        BOOL_NOT                                         ~52     ~51
         78      > JMPZ                                                     ~52, ->81
   30    79    >   FETCH_DIM_R                                      ~53     !6, 2
         80        ASSIGN_OP                                     8          !7, ~53
   32    81    > > RETURN                                                   !7
   33    82*     > RETURN                                                   null

End of function cutmessage

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
130.08 ms | 1411 KiB | 22 Q