3v4l.org

run code in 300+ PHP versions simultaneously
<?php function truncate($text, $length = 100, $ending = '...', $exact = true, $considerHtml = false) { if (is_array ( $ending )) { extract ( $ending ); } if ($considerHtml) { if (strlen ( preg_replace ( '/<.*?>/', '', $text ) ) <= $length) { return $text; } $totalLength = mb_strlen ( $ending ); $openTags = array (); $truncate = ''; preg_match_all ( '/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER ); foreach ( $tags as $tag ) { if (! preg_match ( '/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag [2] )) { if (preg_match ( '/<[\w]+[^>]*>/s', $tag [0] )) { array_unshift ( $openTags, $tag [2] ); } else if (preg_match ( '/<\/([\w]+)[^>]*>/s', $tag [0], $closeTag )) { $pos = array_search ( $closeTag [1], $openTags ); if ($pos !== false) { array_splice ( $openTags, $pos, 1 ); } } } $truncate .= $tag [1]; $contentLength = strlen ( preg_replace ( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $tag [3] ) ); if ($contentLength + $totalLength > $length) { $left = $length - $totalLength; $entitiesLength = 0; if (preg_match_all ( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $tag [3], $entities, PREG_OFFSET_CAPTURE )) { foreach ( $entities [0] as $entity ) { if ($entity [1] + 1 - $entitiesLength <= $left) { $left --; $entitiesLength += strlen ( $entity [0] ); } else { break; } } } $truncate .= substr ( $tag [3], 0, $left + $entitiesLength ); break; } else { $truncate .= $tag [3]; $totalLength += $contentLength; } if ($totalLength >= $length) { break; } } } else { if (strlen ( $text ) <= $length) { return $text; } else { $truncate = substr ( $text, 0, $length - strlen ( $ending ) ); } } if (! $exact) { $spacepos = strrpos ( $truncate, ' ' ); if (isset ( $spacepos )) { if ($considerHtml) { $bits = substr ( $truncate, $spacepos ); preg_match_all ( '/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER ); if (! empty ( $droppedTags )) { foreach ( $droppedTags as $closingTag ) { if (! in_array ( $closingTag [1], $openTags )) { array_unshift ( $openTags, $closingTag [1] ); } } } } $truncate = substr ( $truncate, 0, $spacepos ); } } $truncate .= $ending; if ($considerHtml) { foreach ( $openTags as $tag ) { $truncate .= ''; } } return $truncate; } var_dump(truncate("0123456789ABC", 12));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TnsMK
function name:  (null)
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'truncate'
          2        SEND_VAL                                                 '0123456789ABC'
          3        SEND_VAL                                                 12
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function truncate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 132
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 20
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 77) Position 1 = 33, Position 2 = 130
Branch analysis from position: 33
2 jumps found. (Code = 78) Position 1 = 34, Position 2 = 130
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 73
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 53
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
2 jumps found. (Code = 43) Position 1 = 86, Position 2 = 123
Branch analysis from position: 86
2 jumps found. (Code = 43) Position 1 = 97, Position 2 = 113
Branch analysis from position: 97
2 jumps found. (Code = 77) Position 1 = 99, Position 2 = 112
Branch analysis from position: 99
2 jumps found. (Code = 78) Position 1 = 100, Position 2 = 112
Branch analysis from position: 100
2 jumps found. (Code = 43) Position 1 = 105, Position 2 = 110
Branch analysis from position: 105
1 jumps found. (Code = 42) Position 1 = 111
Branch analysis from position: 111
1 jumps found. (Code = 42) Position 1 = 99
Branch analysis from position: 99
Branch analysis from position: 110
1 jumps found. (Code = 42) Position 1 = 112
Branch analysis from position: 112
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
1 jumps found. (Code = 42) Position 1 = 145
Branch analysis from position: 145
2 jumps found. (Code = 43) Position 1 = 147, Position 2 = 191
Branch analysis from position: 147
2 jumps found. (Code = 43) Position 1 = 154, Position 2 = 191
Branch analysis from position: 154
2 jumps found. (Code = 43) Position 1 = 155, Position 2 = 185
Branch analysis from position: 155
2 jumps found. (Code = 43) Position 1 = 169, Position 2 = 185
Branch analysis from position: 169
2 jumps found. (Code = 77) Position 1 = 170, Position 2 = 184
Branch analysis from position: 170
2 jumps found. (Code = 78) Position 1 = 171, Position 2 = 184
Branch analysis from position: 171
2 jumps found. (Code = 43) Position 1 = 178, Position 2 = 183
Branch analysis from position: 178
1 jumps found. (Code = 42) Position 1 = 170
Branch analysis from position: 170
Branch analysis from position: 183
Branch analysis from position: 184
2 jumps found. (Code = 43) Position 1 = 193, Position 2 = 198
Branch analysis from position: 193
2 jumps found. (Code = 77) Position 1 = 194, Position 2 = 197
Branch analysis from position: 194
2 jumps found. (Code = 78) Position 1 = 195, Position 2 = 197
Branch analysis from position: 195
1 jumps found. (Code = 42) Position 1 = 194
Branch analysis from position: 194
Branch analysis from position: 197
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 197
Branch analysis from position: 198
Branch analysis from position: 184
Branch analysis from position: 185
Branch analysis from position: 185
Branch analysis from position: 191
Branch analysis from position: 191
Branch analysis from position: 112
Branch analysis from position: 112
Branch analysis from position: 113
Branch analysis from position: 123
2 jumps found. (Code = 43) Position 1 = 128, Position 2 = 129
Branch analysis from position: 128
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 129
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 73
Branch analysis from position: 60
2 jumps found. (Code = 43) Position 1 = 68, Position 2 = 73
Branch analysis from position: 68
2 jumps found. (Code = 43) Position 1 = 86, Position 2 = 123
Branch analysis from position: 86
Branch analysis from position: 123
Branch analysis from position: 73
Branch analysis from position: 73
Branch analysis from position: 73
Branch analysis from position: 130
Branch analysis from position: 130
Branch analysis from position: 132
2 jumps found. (Code = 43) Position 1 = 135, Position 2 = 137
Branch analysis from position: 135
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 137
2 jumps found. (Code = 43) Position 1 = 147, Position 2 = 191
Branch analysis from position: 147
Branch analysis from position: 191
Branch analysis from position: 10
filename:       /in/TnsMK
function name:  truncate
number of ops:  200
compiled vars:  !0 = $text, !1 = $length, !2 = $ending, !3 = $exact, !4 = $considerHtml, !5 = $totalLength, !6 = $openTags, !7 = $truncate, !8 = $tags, !9 = $tag, !10 = $closeTag, !11 = $pos, !12 = $contentLength, !13 = $left, !14 = $entitiesLength, !15 = $entities, !16 = $entity, !17 = $spacepos, !18 = $bits, !19 = $droppedTags, !20 = $closingTag
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      100
          2        RECV_INIT                                        !2      '...'
          3        RECV_INIT                                        !3      <true>
          4        RECV_INIT                                        !4      <false>
    3     5        TYPE_CHECK                                  128          !2
          6      > JMPZ                                                     ~21, ->10
    4     7    >   INIT_FCALL                                               'extract'
          8        SEND_REF                                                 !2
          9        DO_ICALL                                                 
    6    10    > > JMPZ                                                     !4, ->132
    7    11    >   INIT_FCALL                                               'preg_replace'
         12        SEND_VAL                                                 '%2F%3C.%2A%3F%3E%2F'
         13        SEND_VAL                                                 ''
         14        SEND_VAR                                                 !0
         15        DO_ICALL                                         $23     
         16        STRLEN                                           ~24     $23
         17        IS_SMALLER_OR_EQUAL                                      ~24, !1
         18      > JMPZ                                                     ~25, ->20
    8    19    > > RETURN                                                   !0
   10    20    >   INIT_FCALL                                               'mb_strlen'
         21        SEND_VAR                                                 !2
         22        DO_ICALL                                         $26     
         23        ASSIGN                                                   !5, $26
   11    24        ASSIGN                                                   !6, <array>
   12    25        ASSIGN                                                   !7, ''
   13    26        INIT_FCALL                                               'preg_match_all'
         27        SEND_VAL                                                 '%2F%28%3C%5C%2F%3F%28%5B%5Cw%2B%5D%2B%29%5B%5E%3E%5D%2A%3E%29%3F%28%5B%5E%3C%3E%5D%2A%29%2F'
         28        SEND_VAR                                                 !0
         29        SEND_REF                                                 !8
         30        SEND_VAL                                                 2
         31        DO_ICALL                                                 
   14    32      > FE_RESET_R                                       $31     !8, ->130
         33    > > FE_FETCH_R                                               $31, !9, ->130
   15    34    >   INIT_FCALL                                               'preg_match'
         35        SEND_VAL                                                 '%2Fimg%7Cbr%7Cinput%7Chr%7Carea%7Cbase%7Cbasefont%7Ccol%7Cframe%7Cisindex%7Clink%7Cmeta%7Cparam%2Fs'
         36        FETCH_DIM_R                                      ~32     !9, 2
         37        SEND_VAL                                                 ~32
         38        DO_ICALL                                         $33     
         39        BOOL_NOT                                         ~34     $33
         40      > JMPZ                                                     ~34, ->73
   16    41    >   INIT_FCALL                                               'preg_match'
         42        SEND_VAL                                                 '%2F%3C%5B%5Cw%5D%2B%5B%5E%3E%5D%2A%3E%2Fs'
         43        FETCH_DIM_R                                      ~35     !9, 0
         44        SEND_VAL                                                 ~35
         45        DO_ICALL                                         $36     
         46      > JMPZ                                                     $36, ->53
   17    47    >   INIT_FCALL                                               'array_unshift'
         48        SEND_REF                                                 !6
         49        FETCH_DIM_R                                      ~37     !9, 2
         50        SEND_VAL                                                 ~37
         51        DO_ICALL                                                 
         52      > JMP                                                      ->73
   18    53    >   INIT_FCALL                                               'preg_match'
         54        SEND_VAL                                                 '%2F%3C%5C%2F%28%5B%5Cw%5D%2B%29%5B%5E%3E%5D%2A%3E%2Fs'
         55        FETCH_DIM_R                                      ~39     !9, 0
         56        SEND_VAL                                                 ~39
         57        SEND_REF                                                 !10
         58        DO_ICALL                                         $40     
         59      > JMPZ                                                     $40, ->73
   19    60    >   INIT_FCALL                                               'array_search'
         61        FETCH_DIM_R                                      ~41     !10, 1
         62        SEND_VAL                                                 ~41
         63        SEND_VAR                                                 !6
         64        DO_ICALL                                         $42     
         65        ASSIGN                                                   !11, $42
   20    66        TYPE_CHECK                                  1018          !11
         67      > JMPZ                                                     ~44, ->73
   21    68    >   INIT_FCALL                                               'array_splice'
         69        SEND_REF                                                 !6
         70        SEND_VAR                                                 !11
         71        SEND_VAL                                                 1
         72        DO_ICALL                                                 
   25    73    >   FETCH_DIM_R                                      ~46     !9, 1
         74        ASSIGN_OP                                     8          !7, ~46
   27    75        INIT_FCALL                                               'preg_replace'
         76        SEND_VAL                                                 '%2F%26%5B0-9a-z%5D%7B2%2C8%7D%3B%7C%26%23%5B0-9%5D%7B1%2C7%7D%3B%7C%5B0-9a-f%5D%7B1%2C6%7D%3B%2Fi'
         77        SEND_VAL                                                 '+'
         78        FETCH_DIM_R                                      ~48     !9, 3
         79        SEND_VAL                                                 ~48
         80        DO_ICALL                                         $49     
         81        STRLEN                                           ~50     $49
         82        ASSIGN                                                   !12, ~50
   28    83        ADD                                              ~52     !12, !5
         84        IS_SMALLER                                               !1, ~52
         85      > JMPZ                                                     ~53, ->123
   29    86    >   SUB                                              ~54     !1, !5
         87        ASSIGN                                                   !13, ~54
   30    88        ASSIGN                                                   !14, 0
   31    89        INIT_FCALL                                               'preg_match_all'
         90        SEND_VAL                                                 '%2F%26%5B0-9a-z%5D%7B2%2C8%7D%3B%7C%26%23%5B0-9%5D%7B1%2C7%7D%3B%7C%5B0-9a-f%5D%7B1%2C6%7D%3B%2Fi'
         91        FETCH_DIM_R                                      ~57     !9, 3
         92        SEND_VAL                                                 ~57
         93        SEND_REF                                                 !15
         94        SEND_VAL                                                 256
         95        DO_ICALL                                         $58     
         96      > JMPZ                                                     $58, ->113
   32    97    >   FETCH_DIM_R                                      ~59     !15, 0
         98      > FE_RESET_R                                       $60     ~59, ->112
         99    > > FE_FETCH_R                                               $60, !16, ->112
   33   100    >   FETCH_DIM_R                                      ~61     !16, 1
        101        ADD                                              ~62     ~61, 1
        102        SUB                                              ~63     ~62, !14
        103        IS_SMALLER_OR_EQUAL                                      ~63, !13
        104      > JMPZ                                                     ~64, ->110
   34   105    >   PRE_DEC                                                  !13
   35   106        FETCH_DIM_R                                      ~66     !16, 0
        107        STRLEN                                           ~67     ~66
        108        ASSIGN_OP                                     1          !14, ~67
        109      > JMP                                                      ->111
   37   110    > > JMP                                                      ->112
   32   111    > > JMP                                                      ->99
        112    >   FE_FREE                                                  $60
   42   113    >   INIT_FCALL                                               'substr'
        114        FETCH_DIM_R                                      ~69     !9, 3
        115        SEND_VAL                                                 ~69
        116        SEND_VAL                                                 0
        117        ADD                                              ~70     !13, !14
        118        SEND_VAL                                                 ~70
        119        DO_ICALL                                         $71     
        120        ASSIGN_OP                                     8          !7, $71
   43   121      > JMP                                                      ->130
        122*       JMP                                                      ->126
   45   123    >   FETCH_DIM_R                                      ~73     !9, 3
        124        ASSIGN_OP                                     8          !7, ~73
   46   125        ASSIGN_OP                                     1          !5, !12
   48   126        IS_SMALLER_OR_EQUAL                                      !1, !5
        127      > JMPZ                                                     ~76, ->129
   49   128    > > JMP                                                      ->130
   14   129    > > JMP                                                      ->33
        130    >   FE_FREE                                                  $31
        131      > JMP                                                      ->145
   54   132    >   STRLEN                                           ~77     !0
        133        IS_SMALLER_OR_EQUAL                                      ~77, !1
        134      > JMPZ                                                     ~78, ->137
   55   135    > > RETURN                                                   !0
        136*       JMP                                                      ->145
   57   137    >   INIT_FCALL                                               'substr'
        138        SEND_VAR                                                 !0
        139        SEND_VAL                                                 0
        140        STRLEN                                           ~79     !2
        141        SUB                                              ~80     !1, ~79
        142        SEND_VAL                                                 ~80
        143        DO_ICALL                                         $81     
        144        ASSIGN                                                   !7, $81
   60   145    >   BOOL_NOT                                         ~83     !3
        146      > JMPZ                                                     ~83, ->191
   61   147    >   INIT_FCALL                                               'strrpos'
        148        SEND_VAR                                                 !7
        149        SEND_VAL                                                 '+'
        150        DO_ICALL                                         $84     
        151        ASSIGN                                                   !17, $84
   62   152        ISSET_ISEMPTY_CV                                         !17
        153      > JMPZ                                                     ~86, ->191
   63   154    > > JMPZ                                                     !4, ->185
   64   155    >   INIT_FCALL                                               'substr'
        156        SEND_VAR                                                 !7
        157        SEND_VAR                                                 !17
        158        DO_ICALL                                         $87     
        159        ASSIGN                                                   !18, $87
   65   160        INIT_FCALL                                               'preg_match_all'
        161        SEND_VAL                                                 '%2F%3C%5C%2F%28%5Ba-z%5D%2B%29%3E%2F'
        162        SEND_VAR                                                 !18
        163        SEND_REF                                                 !19
        164        SEND_VAL                                                 2
        165        DO_ICALL                                                 
   66   166        ISSET_ISEMPTY_CV                                 ~90     !19
        167        BOOL_NOT                                         ~91     ~90
        168      > JMPZ                                                     ~91, ->185
   67   169    > > FE_RESET_R                                       $92     !19, ->184
        170    > > FE_FETCH_R                                               $92, !20, ->184
   68   171    >   INIT_FCALL                                               'in_array'
        172        FETCH_DIM_R                                      ~93     !20, 1
        173        SEND_VAL                                                 ~93
        174        SEND_VAR                                                 !6
        175        DO_ICALL                                         $94     
        176        BOOL_NOT                                         ~95     $94
        177      > JMPZ                                                     ~95, ->183
   69   178    >   INIT_FCALL                                               'array_unshift'
        179        SEND_REF                                                 !6
        180        FETCH_DIM_R                                      ~96     !20, 1
        181        SEND_VAL                                                 ~96
        182        DO_ICALL                                                 
   67   183    > > JMP                                                      ->170
        184    >   FE_FREE                                                  $92
   74   185    >   INIT_FCALL                                               'substr'
        186        SEND_VAR                                                 !7
        187        SEND_VAL                                                 0
        188        SEND_VAR                                                 !17
        189        DO_ICALL                                         $98     
        190        ASSIGN                                                   !7, $98
   78   191    >   ASSIGN_OP                                     8          !7, !2
   80   192      > JMPZ                                                     !4, ->198
   81   193    > > FE_RESET_R                                       $101    !6, ->197
        194    > > FE_FETCH_R                                               $101, !9, ->197
   82   195    >   ASSIGN_OP                                     8          !7, ''
   81   196      > JMP                                                      ->194
        197    >   FE_FREE                                                  $101
   86   198    > > RETURN                                                   !7
   87   199*     > RETURN                                                   null

End of function truncate

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.14 ms | 1419 KiB | 38 Q