3v4l.org

run code in 500+ PHP versions simultaneously
<?php function truncate($string, $width, $on = '[break]') { if (strlen($string) > $width && false !== ($p = strpos(wordwrap($string, $width, $on), $on))) { $string = sprintf('%.'. $p . 's', $string); } return $string; } var_dump(truncate('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', 20)); /* string(36) "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" */ var_dump(truncate("Lorem Ipsum is simply dummy text of the printing and typesetting industry.", 20)); /* string(14) "Lorem Ipsum is" */ var_dump(truncate("Lorem Ipsum\nis simply dummy text of the printing and typesetting industry.", 20)); /* string(14) "Lorem Ipsum is" */ function truncate_strtok($string, $width) { return strtok(wordwrap($string, $width), "\n"); } var_dump(truncate_strtok("Lorem Ipsum\nis simply dummy text of the printing and typesetting industry.", 20)); /* string(11) "Lorem Ipsum" */ var_dump(truncate_strtok("Lorem Ipsum is simply dummy text of the printing and typesetting industry.", 20)); /* string(14) "Lorem Ipsum is" */ function truncate_wrap($string, $width) { if (strlen($string) > $width) { $string = wordwrap($string, $width); $string = substr($string, 0, strpos($string, "\n")); } return $string; } var_dump(truncate_wrap("Lorem Ipsum\nis simply dummy text of the printing and typesetting industry.", 20)); /* string(11) "Lorem Ipsum" */ var_dump(truncate_wrap("Lorem Ipsum is simply dummy text of the printing and typesetting industry.", 20)); /* string(14) "Lorem Ipsum is" */
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FCD21
function name:  (null)
number of ops:  50
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   INIT_FCALL                                                   'var_dump'
          1        INIT_FCALL                                                   'truncate'
          2        SEND_VAL                                                     '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
          3        SEND_VAL                                                     20
          4        DO_FCALL                                          0  $0      
          5        SEND_VAR                                                     $0
          6        DO_ICALL                                                     
   15     7        INIT_FCALL                                                   'var_dump'
          8        INIT_FCALL                                                   'truncate'
          9        SEND_VAL                                                     'Lorem+Ipsum+is+simply+dummy+text+of+the+printing+and+typesetting+industry.'
         10        SEND_VAL                                                     20
         11        DO_FCALL                                          0  $2      
         12        SEND_VAR                                                     $2
         13        DO_ICALL                                                     
   20    14        INIT_FCALL                                                   'var_dump'
         15        INIT_FCALL                                                   'truncate'
         16        SEND_VAL                                                     'Lorem+Ipsum%0Ais+simply+dummy+text+of+the+printing+and+typesetting+industry.'
         17        SEND_VAL                                                     20
         18        DO_FCALL                                          0  $4      
         19        SEND_VAR                                                     $4
         20        DO_ICALL                                                     
   30    21        INIT_FCALL                                                   'var_dump'
         22        INIT_FCALL                                                   'truncate_strtok'
         23        SEND_VAL                                                     'Lorem+Ipsum%0Ais+simply+dummy+text+of+the+printing+and+typesetting+industry.'
         24        SEND_VAL                                                     20
         25        DO_FCALL                                          0  $6      
         26        SEND_VAR                                                     $6
         27        DO_ICALL                                                     
   35    28        INIT_FCALL                                                   'var_dump'
         29        INIT_FCALL                                                   'truncate_strtok'
         30        SEND_VAL                                                     'Lorem+Ipsum+is+simply+dummy+text+of+the+printing+and+typesetting+industry.'
         31        SEND_VAL                                                     20
         32        DO_FCALL                                          0  $8      
         33        SEND_VAR                                                     $8
         34        DO_ICALL                                                     
   49    35        INIT_FCALL                                                   'var_dump'
         36        INIT_FCALL                                                   'truncate_wrap'
         37        SEND_VAL                                                     'Lorem+Ipsum%0Ais+simply+dummy+text+of+the+printing+and+typesetting+industry.'
         38        SEND_VAL                                                     20
         39        DO_FCALL                                          0  $10     
         40        SEND_VAR                                                     $10
         41        DO_ICALL                                                     
   54    42        INIT_FCALL                                                   'var_dump'
         43        INIT_FCALL                                                   'truncate_wrap'
         44        SEND_VAL                                                     'Lorem+Ipsum+is+simply+dummy+text+of+the+printing+and+typesetting+industry.'
         45        SEND_VAL                                                     20
         46        DO_FCALL                                          0  $12     
         47        SEND_VAR                                                     $12
         48        DO_ICALL                                                     
   57    49      > RETURN                                                       1

Function truncate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 6, Position 2 = 15
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 23
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
Branch analysis from position: 15
filename:       /in/FCD21
function name:  truncate
number of ops:  25
compiled vars:  !0 = $string, !1 = $width, !2 = $on, !3 = $p
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV_INIT                                            !2      '%5Bbreak%5D'
    4     3        STRLEN                                               ~4      !0
          4        IS_SMALLER                                           ~5      !1, ~4
          5      > JMPZ_EX                                              ~5      ~5, ->15
          6    >   INIT_FCALL                                                   'wordwrap'
          7        SEND_VAR                                                     !0
          8        SEND_VAR                                                     !1
          9        SEND_VAR                                                     !2
         10        DO_ICALL                                             $6      
         11        FRAMELESS_ICALL_2                strpos              ~7      $6, !2
         12        ASSIGN                                               ~8      !3, ~7
         13        TYPE_CHECK                                      1018  ~9      ~8
         14        BOOL                                                 ~5      ~9
         15    > > JMPZ                                                         ~5, ->23
    5    16    >   INIT_FCALL                                                   'sprintf'
         17        CONCAT                                               ~10     '%25.', !3
         18        CONCAT                                               ~11     ~10, 's'
         19        SEND_VAL                                                     ~11
         20        SEND_VAR                                                     !0
         21        DO_ICALL                                             $12     
         22        ASSIGN                                                       !0, $12
    7    23    > > RETURN                                                       !0
    8    24*     > RETURN                                                       null

End of function truncate

Function truncate_strtok:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FCD21
function name:  truncate_strtok
number of ops:  12
compiled vars:  !0 = $string, !1 = $width
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   26     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   27     2        INIT_FCALL                                                   'strtok'
          3        INIT_FCALL                                                   'wordwrap'
          4        SEND_VAR                                                     !0
          5        SEND_VAR                                                     !1
          6        DO_ICALL                                             $2      
          7        SEND_VAR                                                     $2
          8        SEND_VAL                                                     '%0A'
          9        DO_ICALL                                             $3      
         10      > RETURN                                                       $3
   28    11*     > RETURN                                                       null

End of function truncate_strtok

Function truncate_wrap:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/FCD21
function name:  truncate_wrap
number of ops:  16
compiled vars:  !0 = $string, !1 = $width
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   40     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   41     2        STRLEN                                               ~2      !0
          3        IS_SMALLER                                                   !1, ~2
          4      > JMPZ                                                         ~3, ->14
   42     5    >   INIT_FCALL                                                   'wordwrap'
          6        SEND_VAR                                                     !0
          7        SEND_VAR                                                     !1
          8        DO_ICALL                                             $4      
          9        ASSIGN                                                       !0, $4
   43    10        FRAMELESS_ICALL_2                strpos              ~6      !0, '%0A'
         11        FRAMELESS_ICALL_3                substr              ~7      !0, 0
         12        OP_DATA                                                      ~6
         13        ASSIGN                                                       !0, ~7
   46    14    > > RETURN                                                       !0
   47    15*     > RETURN                                                       null

End of function truncate_wrap

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
170.47 ms | 3388 KiB | 24 Q