3v4l.org

run code in 300+ PHP versions simultaneously
<?php function truncate($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) { if ($considerHtml) { // if the plain text is shorter than the maximum length, return the whole text if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) { return $text; } // splits all html-tags to scanable lines preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); $total_length = strlen($ending); $open_tags = array(); $truncate = ''; foreach ($lines as $line_matchings) { // if there is any html-tag in this line, handle it and add it (uncounted) to the output if (!empty($line_matchings[1])) { // if it's an "empty element" with or without xhtml-conform closing slash if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { // do nothing // if tag is a closing tag } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { // delete tag from $open_tags list $pos = array_search($tag_matchings[1], $open_tags); if ($pos !== false) { unset($open_tags[$pos]); } // if tag is an opening tag } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) { // add tag to the beginning of $open_tags list array_unshift($open_tags, strtolower($tag_matchings[1])); } // add html-tag to $truncate'd text $truncate .= $line_matchings[1]; } // calculate the length of the plain text part of the line; handle entities as one character $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); if ($total_length+$content_length> $length) { // the number of characters which are left $left = $length - $total_length; $entities_length = 0; // search for html entities if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) { // calculate the real length of all entities in the legal range foreach ($entities[0] as $entity) { if ($entity[1]+1-$entities_length <= $left) { $left--; $entities_length += strlen($entity[0]); } else { // no more characters left break; } } } $truncate .= substr($line_matchings[2], 0, $left+$entities_length); // maximum lenght is reached, so get off the loop break; } else { $truncate .= $line_matchings[2]; $total_length += $content_length; } // if the maximum length is reached, get off the loop if($total_length>= $length) { break; } } } else { if (strlen($text) <= $length) { return $text; } else { $truncate = substr($text, 0, $length - strlen($ending)); } } // if the words shouldn't be cut in the middle... if (!$exact) { // ...search the last occurance of a space... $spacepos = strrpos($truncate, ' '); if (isset($spacepos)) { // ...and cut the text in this position $truncate = substr($truncate, 0, $spacepos); } } // add the defined ending to the text $truncate .= $ending; if($considerHtml) { // close all unclosed html-tags foreach ($open_tags as $tag) { $truncate .= '</' . $tag . '>'; } } return $truncate; } truncate('<b>&lt;Hello&gt;</b> <img src="world.png" alt="" /> world!', 10); print("\n"); truncate('<table><tr><td>Heck, </td><td>throw</td></tr><tr><td>in a</td><td>table</td></tr></table>', 10); print("\n"); truncate("<em><b>Hello</b>&#20;w\xC3\xB8rld!</em>", 10); print("\n");
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8Fmr2
function name:  (null)
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   95     0  E >   INIT_FCALL                                               'truncate'
          1        SEND_VAL                                                 '%3Cb%3E%26lt%3BHello%26gt%3B%3C%2Fb%3E+%3Cimg+src%3D%22world.png%22+alt%3D%22%22+%2F%3E+world%21'
          2        SEND_VAL                                                 10
          3        DO_FCALL                                      0          
          4        ECHO                                                     '%0A'
   97     5        INIT_FCALL                                               'truncate'
          6        SEND_VAL                                                 '%3Ctable%3E%3Ctr%3E%3Ctd%3EHeck%2C+%3C%2Ftd%3E%3Ctd%3Ethrow%3C%2Ftd%3E%3C%2Ftr%3E%3Ctr%3E%3Ctd%3Ein+a%3C%2Ftd%3E%3Ctd%3Etable%3C%2Ftd%3E%3C%2Ftr%3E%3C%2Ftable%3E'
          7        SEND_VAL                                                 10
          8        DO_FCALL                                      0          
          9        ECHO                                                     '%0A'
   99    10        INIT_FCALL                                               'truncate'
         11        SEND_VAL                                                 '%3Cem%3E%3Cb%3EHello%3C%2Fb%3E%26%2320%3Bw%C3%B8rld%21%3C%2Fem%3E'
         12        SEND_VAL                                                 10
         13        DO_FCALL                                      0          
         14        ECHO                                                     '%0A'
         15      > RETURN                                                   1

Function truncate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 128
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 77) Position 1 = 26, Position 2 = 126
Branch analysis from position: 26
2 jumps found. (Code = 78) Position 1 = 27, Position 2 = 126
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 71
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 37
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
2 jumps found. (Code = 43) Position 1 = 82, Position 2 = 119
Branch analysis from position: 82
2 jumps found. (Code = 43) Position 1 = 93, Position 2 = 109
Branch analysis from position: 93
2 jumps found. (Code = 77) Position 1 = 95, Position 2 = 108
Branch analysis from position: 95
2 jumps found. (Code = 78) Position 1 = 96, Position 2 = 108
Branch analysis from position: 96
2 jumps found. (Code = 43) Position 1 = 101, Position 2 = 106
Branch analysis from position: 101
1 jumps found. (Code = 42) Position 1 = 107
Branch analysis from position: 107
1 jumps found. (Code = 42) Position 1 = 95
Branch analysis from position: 95
Branch analysis from position: 106
1 jumps found. (Code = 42) Position 1 = 108
Branch analysis from position: 108
1 jumps found. (Code = 42) Position 1 = 126
Branch analysis from position: 126
1 jumps found. (Code = 42) Position 1 = 141
Branch analysis from position: 141
2 jumps found. (Code = 43) Position 1 = 143, Position 2 = 156
Branch analysis from position: 143
2 jumps found. (Code = 43) Position 1 = 150, Position 2 = 156
Branch analysis from position: 150
2 jumps found. (Code = 43) Position 1 = 158, Position 2 = 165
Branch analysis from position: 158
2 jumps found. (Code = 77) Position 1 = 159, Position 2 = 164
Branch analysis from position: 159
2 jumps found. (Code = 78) Position 1 = 160, Position 2 = 164
Branch analysis from position: 160
1 jumps found. (Code = 42) Position 1 = 159
Branch analysis from position: 159
Branch analysis from position: 164
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 164
Branch analysis from position: 165
Branch analysis from position: 156
Branch analysis from position: 156
Branch analysis from position: 108
Branch analysis from position: 108
Branch analysis from position: 109
Branch analysis from position: 119
2 jumps found. (Code = 43) Position 1 = 124, Position 2 = 125
Branch analysis from position: 124
1 jumps found. (Code = 42) Position 1 = 126
Branch analysis from position: 126
Branch analysis from position: 125
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 54
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 53
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
Branch analysis from position: 53
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 61, Position 2 = 69
Branch analysis from position: 61
2 jumps found. (Code = 43) Position 1 = 82, Position 2 = 119
Branch analysis from position: 82
Branch analysis from position: 119
Branch analysis from position: 69
Branch analysis from position: 71
Branch analysis from position: 126
Branch analysis from position: 126
Branch analysis from position: 128
2 jumps found. (Code = 43) Position 1 = 131, Position 2 = 133
Branch analysis from position: 131
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 133
2 jumps found. (Code = 43) Position 1 = 143, Position 2 = 156
Branch analysis from position: 143
Branch analysis from position: 156
filename:       /in/8Fmr2
function name:  truncate
number of ops:  167
compiled vars:  !0 = $text, !1 = $length, !2 = $ending, !3 = $exact, !4 = $considerHtml, !5 = $lines, !6 = $total_length, !7 = $open_tags, !8 = $truncate, !9 = $line_matchings, !10 = $tag_matchings, !11 = $pos, !12 = $content_length, !13 = $left, !14 = $entities_length, !15 = $entities, !16 = $entity, !17 = $spacepos, !18 = $tag
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      100
          2        RECV_INIT                                        !2      '...'
          3        RECV_INIT                                        !3      <false>
          4        RECV_INIT                                        !4      <true>
    4     5      > JMPZ                                                     !4, ->128
    6     6    >   INIT_FCALL                                               'preg_replace'
          7        SEND_VAL                                                 '%2F%3C.%2A%3F%3E%2F'
          8        SEND_VAL                                                 ''
          9        SEND_VAR                                                 !0
         10        DO_ICALL                                         $19     
         11        STRLEN                                           ~20     $19
         12        IS_SMALLER_OR_EQUAL                                      ~20, !1
         13      > JMPZ                                                     ~21, ->15
    7    14    > > RETURN                                                   !0
   10    15    >   INIT_FCALL                                               'preg_match_all'
         16        SEND_VAL                                                 '%2F%28%3C.%2B%3F%3E%29%3F%28%5B%5E%3C%3E%5D%2A%29%2Fs'
         17        SEND_VAR                                                 !0
         18        SEND_REF                                                 !5
         19        SEND_VAL                                                 2
         20        DO_ICALL                                                 
   11    21        STRLEN                                           ~23     !2
         22        ASSIGN                                                   !6, ~23
   12    23        ASSIGN                                                   !7, <array>
   13    24        ASSIGN                                                   !8, ''
   14    25      > FE_RESET_R                                       $27     !5, ->126
         26    > > FE_FETCH_R                                               $27, !9, ->126
   16    27    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~28     !9, 1
         28        BOOL_NOT                                         ~29     ~28
         29      > JMPZ                                                     ~29, ->71
   18    30    >   INIT_FCALL                                               'preg_match'
         31        SEND_VAL                                                 '%2F%5E%3C%28%5Cs%2A.%2B%3F%5C%2F%5Cs%2A%7C%5Cs%2A%28img%7Cbr%7Cinput%7Chr%7Carea%7Cbase%7Cbasefont%7Ccol%7Cframe%7Cisindex%7Clink%7Cmeta%7Cparam%29%28%5Cs.%2B%3F%29%3F%29%3E%24%2Fis'
         32        FETCH_DIM_R                                      ~30     !9, 1
         33        SEND_VAL                                                 ~30
         34        DO_ICALL                                         $31     
         35      > JMPZ                                                     $31, ->37
         36    > > JMP                                                      ->69
   21    37    >   INIT_FCALL                                               'preg_match'
         38        SEND_VAL                                                 '%2F%5E%3C%5Cs%2A%5C%2F%28%5B%5E%5Cs%5D%2B%3F%29%5Cs%2A%3E%24%2Fs'
         39        FETCH_DIM_R                                      ~32     !9, 1
         40        SEND_VAL                                                 ~32
         41        SEND_REF                                                 !10
         42        DO_ICALL                                         $33     
         43      > JMPZ                                                     $33, ->54
   23    44    >   INIT_FCALL                                               'array_search'
         45        FETCH_DIM_R                                      ~34     !10, 1
         46        SEND_VAL                                                 ~34
         47        SEND_VAR                                                 !7
         48        DO_ICALL                                         $35     
         49        ASSIGN                                                   !11, $35
   24    50        TYPE_CHECK                                  1018          !11
         51      > JMPZ                                                     ~37, ->53
   25    52    >   UNSET_DIM                                                !7, !11
         53    > > JMP                                                      ->69
   28    54    >   INIT_FCALL                                               'preg_match'
         55        SEND_VAL                                                 '%2F%5E%3C%5Cs%2A%28%5B%5E%5Cs%3E%21%5D%2B%29.%2A%3F%3E%24%2Fs'
         56        FETCH_DIM_R                                      ~38     !9, 1
         57        SEND_VAL                                                 ~38
         58        SEND_REF                                                 !10
         59        DO_ICALL                                         $39     
         60      > JMPZ                                                     $39, ->69
   30    61    >   INIT_FCALL                                               'array_unshift'
         62        SEND_REF                                                 !7
         63        INIT_FCALL                                               'strtolower'
         64        FETCH_DIM_R                                      ~40     !10, 1
         65        SEND_VAL                                                 ~40
         66        DO_ICALL                                         $41     
         67        SEND_VAR                                                 $41
         68        DO_ICALL                                                 
   33    69    >   FETCH_DIM_R                                      ~43     !9, 1
         70        ASSIGN_OP                                     8          !8, ~43
   36    71    >   INIT_FCALL                                               'preg_replace'
         72        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'
         73        SEND_VAL                                                 '+'
         74        FETCH_DIM_R                                      ~45     !9, 2
         75        SEND_VAL                                                 ~45
         76        DO_ICALL                                         $46     
         77        STRLEN                                           ~47     $46
         78        ASSIGN                                                   !12, ~47
   37    79        ADD                                              ~49     !6, !12
         80        IS_SMALLER                                               !1, ~49
         81      > JMPZ                                                     ~50, ->119
   39    82    >   SUB                                              ~51     !1, !6
         83        ASSIGN                                                   !13, ~51
   40    84        ASSIGN                                                   !14, 0
   42    85        INIT_FCALL                                               'preg_match_all'
         86        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'
         87        FETCH_DIM_R                                      ~54     !9, 2
         88        SEND_VAL                                                 ~54
         89        SEND_REF                                                 !15
         90        SEND_VAL                                                 256
         91        DO_ICALL                                         $55     
         92      > JMPZ                                                     $55, ->109
   44    93    >   FETCH_DIM_R                                      ~56     !15, 0
         94      > FE_RESET_R                                       $57     ~56, ->108
         95    > > FE_FETCH_R                                               $57, !16, ->108
   45    96    >   FETCH_DIM_R                                      ~58     !16, 1
         97        ADD                                              ~59     ~58, 1
         98        SUB                                              ~60     ~59, !14
         99        IS_SMALLER_OR_EQUAL                                      ~60, !13
        100      > JMPZ                                                     ~61, ->106
   46   101    >   PRE_DEC                                                  !13
   47   102        FETCH_DIM_R                                      ~63     !16, 0
        103        STRLEN                                           ~64     ~63
        104        ASSIGN_OP                                     1          !14, ~64
        105      > JMP                                                      ->107
   50   106    > > JMP                                                      ->108
   44   107    > > JMP                                                      ->95
        108    >   FE_FREE                                                  $57
   54   109    >   INIT_FCALL                                               'substr'
        110        FETCH_DIM_R                                      ~66     !9, 2
        111        SEND_VAL                                                 ~66
        112        SEND_VAL                                                 0
        113        ADD                                              ~67     !13, !14
        114        SEND_VAL                                                 ~67
        115        DO_ICALL                                         $68     
        116        ASSIGN_OP                                     8          !8, $68
   56   117      > JMP                                                      ->126
        118*       JMP                                                      ->122
   58   119    >   FETCH_DIM_R                                      ~70     !9, 2
        120        ASSIGN_OP                                     8          !8, ~70
   59   121        ASSIGN_OP                                     1          !6, !12
   62   122        IS_SMALLER_OR_EQUAL                                      !1, !6
        123      > JMPZ                                                     ~73, ->125
   63   124    > > JMP                                                      ->126
   14   125    > > JMP                                                      ->26
        126    >   FE_FREE                                                  $27
        127      > JMP                                                      ->141
   67   128    >   STRLEN                                           ~74     !0
        129        IS_SMALLER_OR_EQUAL                                      ~74, !1
        130      > JMPZ                                                     ~75, ->133
   68   131    > > RETURN                                                   !0
        132*       JMP                                                      ->141
   70   133    >   INIT_FCALL                                               'substr'
        134        SEND_VAR                                                 !0
        135        SEND_VAL                                                 0
        136        STRLEN                                           ~76     !2
        137        SUB                                              ~77     !1, ~76
        138        SEND_VAL                                                 ~77
        139        DO_ICALL                                         $78     
        140        ASSIGN                                                   !8, $78
   74   141    >   BOOL_NOT                                         ~80     !3
        142      > JMPZ                                                     ~80, ->156
   76   143    >   INIT_FCALL                                               'strrpos'
        144        SEND_VAR                                                 !8
        145        SEND_VAL                                                 '+'
        146        DO_ICALL                                         $81     
        147        ASSIGN                                                   !17, $81
   77   148        ISSET_ISEMPTY_CV                                         !17
        149      > JMPZ                                                     ~83, ->156
   79   150    >   INIT_FCALL                                               'substr'
        151        SEND_VAR                                                 !8
        152        SEND_VAL                                                 0
        153        SEND_VAR                                                 !17
        154        DO_ICALL                                         $84     
        155        ASSIGN                                                   !8, $84
   83   156    >   ASSIGN_OP                                     8          !8, !2
   84   157      > JMPZ                                                     !4, ->165
   86   158    > > FE_RESET_R                                       $87     !7, ->164
        159    > > FE_FETCH_R                                               $87, !18, ->164
   87   160    >   CONCAT                                           ~88     '%3C%2F', !18
        161        CONCAT                                           ~89     ~88, '%3E'
        162        ASSIGN_OP                                     8          !8, ~89
   86   163      > JMP                                                      ->159
        164    >   FE_FREE                                                  $87
   90   165    > > RETURN                                                   !8
   91   166*     > RETURN                                                   null

End of function truncate

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
185.33 ms | 1419 KiB | 32 Q