3v4l.org

run code in 300+ 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 = 18
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 26
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
Branch analysis from position: 18
filename:       /in/FCD21
function name:  truncate
number of ops:  28
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, ->18
          6    >   INIT_FCALL                                               'strpos'
          7        INIT_FCALL                                               'wordwrap'
          8        SEND_VAR                                                 !0
          9        SEND_VAR                                                 !1
         10        SEND_VAR                                                 !2
         11        DO_ICALL                                         $6      
         12        SEND_VAR                                                 $6
         13        SEND_VAR                                                 !2
         14        DO_ICALL                                         $7      
         15        ASSIGN                                           ~8      !3, $7
         16        TYPE_CHECK                                  1018  ~9      ~8
         17        BOOL                                             ~5      ~9
         18    > > JMPZ                                                     ~5, ->26
    5    19    >   INIT_FCALL                                               'sprintf'
         20        CONCAT                                           ~10     '%25.', !3
         21        CONCAT                                           ~11     ~10, 's'
         22        SEND_VAL                                                 ~11
         23        SEND_VAR                                                 !0
         24        DO_ICALL                                         $12     
         25        ASSIGN                                                   !0, $12
    7    26    > > RETURN                                                   !0
    8    27*     > 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 = 20
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/FCD21
function name:  truncate_wrap
number of ops:  22
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, ->20
   42     5    >   INIT_FCALL                                               'wordwrap'
          6        SEND_VAR                                                 !0
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                         $4      
          9        ASSIGN                                                   !0, $4
   43    10        INIT_FCALL                                               'substr'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 0
         13        INIT_FCALL                                               'strpos'
         14        SEND_VAR                                                 !0
         15        SEND_VAL                                                 '%0A'
         16        DO_ICALL                                         $6      
         17        SEND_VAR                                                 $6
         18        DO_ICALL                                         $7      
         19        ASSIGN                                                   !0, $7
   46    20    > > RETURN                                                   !0
   47    21*     > RETURN                                                   null

End of function truncate_wrap

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.55 ms | 1014 KiB | 26 Q