3v4l.org

run code in 300+ PHP versions simultaneously
<?php $text = <<<TEXT Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec placerat libero id mi facilisis, at sagittis tortor porta. Donec eget sodales ipsum. Donec sagittis lacus mauris, et efficitur quam porttitor eu. Fusce eget consequat purus. Maecenas rutrum arcu viverra est rhoncus, et hendrerit tellus elementum. Aenean ornare dolor tempus ante porta, sit amet convallis lacus rutrum. Maecenas bibendum magna tortor. Vestibulum tortor nunc, dictum vitae nisl quis, pharetra mattis massa. Vestibulum vulputate leo eros, eget maximus ipsum tristique quis. Quisque rutrum vel felis eget feugiat. Etiam interdum nisi ac nibh egestas malesuada. Mauris fringilla nisi id rutrum fermentum. Ut ultrices ipsum rutrum, hendrerit urna non, dapibus ligula. Vivamus rhoncus eros eget eros feugiat volutpat. In ac arcu at purus porta varius. Sed commodo diam a ipsum vestibulum, et sagittis sem consectetur. TEXT; $needles = [ 'efficitur', 'eget', 'Ipsum', ]; array_multisort( array_map('strlen', $needles), SORT_DESC, $needles ); $pattern = '#\b(?>' . implode('|', array_map('preg_quote', $needles)) . ')\b#i'; if (preg_match_all($pattern, $text, $m, PREG_OFFSET_CAPTURE)) { $narrowestWordOffsets = []; $fullSetCount = null; $totalLength = null; foreach ($m[0] as $i => [$w, $o]) { $set = [$w => $o]; foreach (array_slice($m[0], $i + 1) as [$w2, $o2]) { if (!isset($set[$w2])) { $set[$w2] = $o2; $lastWord = $w2; } } $setLength = strlen($lastWord) + $set[$lastWord] - current($set); if ( !$narrowestWordOffsets || ( $setLength < $totalLength && count($set) === $fullSetCount ) ) { $narrowestWordOffsets = $set; $totalLength = $setLength; $fullSetCount ??= count($set); } } var_export( preg_replace_callback( '#(.*?)([^.]*' . preg_quote(substr($text, current($narrowestWordOffsets), $totalLength)) . '[^.]*\.?)(.*)#', fn($m) => (strlen($m[1]) ? '...' : '') . $m[2] . (strlen($m[3]) ? ' ...' : ''), $text ) ); } else { echo 'No needles found in haystack'; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 113
Branch analysis from position: 29
2 jumps found. (Code = 77) Position 1 = 34, Position 2 = 89
Branch analysis from position: 34
2 jumps found. (Code = 78) Position 1 = 35, Position 2 = 89
Branch analysis from position: 35
2 jumps found. (Code = 77) Position 1 = 50, Position 2 = 63
Branch analysis from position: 50
2 jumps found. (Code = 78) Position 1 = 51, Position 2 = 63
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 62
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 62
Branch analysis from position: 63
2 jumps found. (Code = 47) Position 1 = 74, Position 2 = 80
Branch analysis from position: 74
2 jumps found. (Code = 46) Position 1 = 76, Position 2 = 79
Branch analysis from position: 76
2 jumps found. (Code = 43) Position 1 = 81, Position 2 = 88
Branch analysis from position: 81
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 88
Branch analysis from position: 79
Branch analysis from position: 80
Branch analysis from position: 63
Branch analysis from position: 89
1 jumps found. (Code = 42) Position 1 = 114
Branch analysis from position: 114
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 89
Branch analysis from position: 113
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D7NK2
function name:  (null)
number of ops:  115
compiled vars:  !0 = $text, !1 = $needles, !2 = $pattern, !3 = $m, !4 = $narrowestWordOffsets, !5 = $fullSetCount, !6 = $totalLength, !7 = $w, !8 = $o, !9 = $i, !10 = $set, !11 = $w2, !12 = $o2, !13 = $lastWord, !14 = $setLength
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'Lorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit.+Donec+placerat+libero+id+mi+facilisis%2C+at+sagittis+tortor+porta.+Donec+eget+sodales+ipsum.+Donec+sagittis+lacus+mauris%2C+et+efficitur+quam+porttitor+eu.+Fusce+eget+consequat+purus.+Maecenas+rutrum+arcu+viverra+est+rhoncus%2C+et+hendrerit+tellus+elementum.+Aenean+ornare+dolor+tempus+ante+porta%2C+sit+amet+convallis+lacus+rutrum.+Maecenas+bibendum+magna+tortor.+Vestibulum+tortor+nunc%2C+dictum+vitae+nisl+quis%2C+pharetra+mattis+massa.+Vestibulum+vulputate+leo+eros%2C+eget+maximus+ipsum+tristique+quis.+Quisque+rutrum+vel+felis+eget+feugiat.+Etiam+interdum+nisi+ac+nibh+egestas+malesuada.+Mauris+fringilla+nisi+id+rutrum+fermentum.+Ut+ultrices+ipsum+rutrum%2C+hendrerit+urna+non%2C+dapibus+ligula.+Vivamus+rhoncus+eros+eget+eros+feugiat+volutpat.+In+ac+arcu+at+purus+porta+varius.+Sed+commodo+diam+a+ipsum+vestibulum%2C+et+sagittis+sem+consectetur.'
    7     1        ASSIGN                                                   !1, <array>
   13     2        INIT_FCALL                                               'array_multisort'
   14     3        INIT_FCALL                                               'array_map'
          4        SEND_VAL                                                 'strlen'
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $17     
          7        SEND_VAL                                                 $17
   15     8        SEND_VAL                                                 3
   16     9        SEND_REF                                                 !1
   13    10        DO_ICALL                                                 
   19    11        INIT_FCALL                                               'implode'
         12        SEND_VAL                                                 '%7C'
         13        INIT_FCALL                                               'array_map'
         14        SEND_VAL                                                 'preg_quote'
         15        SEND_VAR                                                 !1
         16        DO_ICALL                                         $19     
         17        SEND_VAR                                                 $19
         18        DO_ICALL                                         $20     
         19        CONCAT                                           ~21     '%23%5Cb%28%3F%3E', $20
         20        CONCAT                                           ~22     ~21, '%29%5Cb%23i'
         21        ASSIGN                                                   !2, ~22
   21    22        INIT_FCALL                                               'preg_match_all'
         23        SEND_VAR                                                 !2
         24        SEND_VAR                                                 !0
         25        SEND_REF                                                 !3
         26        SEND_VAL                                                 256
         27        DO_ICALL                                         $24     
         28      > JMPZ                                                     $24, ->113
   22    29    >   ASSIGN                                                   !4, <array>
   23    30        ASSIGN                                                   !5, null
   24    31        ASSIGN                                                   !6, null
   25    32        FETCH_DIM_R                                      ~28     !3, 0
         33      > FE_RESET_R                                       $29     ~28, ->89
         34    > > FE_FETCH_R                                       ~35     $29, $30, ->89
         35    >   FETCH_LIST_R                                     $31     $30, 0
         36        ASSIGN                                                   !7, $31
         37        FETCH_LIST_R                                     $33     $30, 1
         38        ASSIGN                                                   !8, $33
         39        FREE                                                     $30
         40        ASSIGN                                                   !9, ~35
   26    41        INIT_ARRAY                                       ~37     !8, !7
         42        ASSIGN                                                   !10, ~37
   27    43        INIT_FCALL                                               'array_slice'
         44        FETCH_DIM_R                                      ~39     !3, 0
         45        SEND_VAL                                                 ~39
         46        ADD                                              ~40     !9, 1
         47        SEND_VAL                                                 ~40
         48        DO_ICALL                                         $41     
         49      > FE_RESET_R                                       $42     $41, ->63
         50    > > FE_FETCH_R                                               $42, $43, ->63
         51    >   FETCH_LIST_R                                     $44     $43, 0
         52        ASSIGN                                                   !11, $44
         53        FETCH_LIST_R                                     $46     $43, 1
         54        ASSIGN                                                   !12, $46
         55        FREE                                                     $43
   28    56        ISSET_ISEMPTY_DIM_OBJ                         0  ~48     !10, !11
         57        BOOL_NOT                                         ~49     ~48
         58      > JMPZ                                                     ~49, ->62
   29    59    >   ASSIGN_DIM                                               !10, !11
         60        OP_DATA                                                  !12
   30    61        ASSIGN                                                   !13, !11
   27    62    > > JMP                                                      ->50
         63    >   FE_FREE                                                  $42
   33    64        STRLEN                                           ~52     !13
         65        FETCH_DIM_R                                      ~53     !10, !13
         66        ADD                                              ~54     ~52, ~53
         67        INIT_FCALL                                               'current'
         68        SEND_VAR                                                 !10
         69        DO_ICALL                                         $55     
         70        SUB                                              ~56     ~54, $55
         71        ASSIGN                                                   !14, ~56
   35    72        BOOL_NOT                                         ~58     !4
         73      > JMPNZ_EX                                         ~58     ~58, ->80
   37    74    >   IS_SMALLER                                       ~59     !14, !6
         75      > JMPZ_EX                                          ~59     ~59, ->79
   38    76    >   COUNT                                            ~60     !10
         77        IS_IDENTICAL                                     ~61     !5, ~60
         78        BOOL                                             ~59     ~61
         79    >   BOOL                                             ~58     ~59
         80    > > JMPZ                                                     ~58, ->88
   41    81    >   ASSIGN                                                   !4, !10
   42    82        ASSIGN                                                   !6, !14
   43    83        COALESCE                                         ~64     !5
         84        COUNT                                            ~65     !10
         85        ASSIGN                                           ~66     !5, ~65
         86        QM_ASSIGN                                        ~64     ~66
         87        FREE                                                     ~64
   25    88    > > JMP                                                      ->34
         89    >   FE_FREE                                                  $29
   46    90        INIT_FCALL                                               'var_export'
   47    91        INIT_FCALL                                               'preg_replace_callback'
   48    92        INIT_FCALL                                               'preg_quote'
         93        INIT_FCALL                                               'substr'
         94        SEND_VAR                                                 !0
         95        INIT_FCALL                                               'current'
         96        SEND_VAR                                                 !4
         97        DO_ICALL                                         $67     
         98        SEND_VAR                                                 $67
         99        SEND_VAR                                                 !6
        100        DO_ICALL                                         $68     
        101        SEND_VAR                                                 $68
        102        DO_ICALL                                         $69     
        103        CONCAT                                           ~70     '%23%28.%2A%3F%29%28%5B%5E.%5D%2A', $69
        104        CONCAT                                           ~71     ~70, '%5B%5E.%5D%2A%5C.%3F%29%28.%2A%29%23'
        105        SEND_VAL                                                 ~71
   49   106        DECLARE_LAMBDA_FUNCTION                          ~72     [0]
   52   107        SEND_VAL                                                 ~72
   53   108        SEND_VAR                                                 !0
   47   109        DO_ICALL                                         $73     
   53   110        SEND_VAR                                                 $73
   46   111        DO_ICALL                                                 
   21   112      > JMP                                                      ->114
   57   113    >   ECHO                                                     'No+needles+found+in+haystack'
   58   114    > > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
Branch analysis from position: 14
filename:       /in/D7NK2
function name:  {closure}
number of ops:  18
compiled vars:  !0 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
   50     1        FETCH_DIM_R                                      ~1      !0, 1
          2        STRLEN                                           ~2      ~1
          3      > JMPZ                                                     ~2, ->6
          4    >   QM_ASSIGN                                        ~3      '...'
          5      > JMP                                                      ->7
          6    >   QM_ASSIGN                                        ~3      ''
   51     7    >   FETCH_DIM_R                                      ~4      !0, 2
          8        CONCAT                                           ~5      ~3, ~4
   52     9        FETCH_DIM_R                                      ~6      !0, 3
         10        STRLEN                                           ~7      ~6
         11      > JMPZ                                                     ~7, ->14
         12    >   QM_ASSIGN                                        ~8      '+...'
         13      > JMP                                                      ->15
         14    >   QM_ASSIGN                                        ~8      ''
         15    >   CONCAT                                           ~9      ~5, ~8
         16      > RETURN                                                   ~9
         17*     > RETURN                                                   null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
253.18 ms | 1033 KiB | 23 Q