3v4l.org

run code in 300+ PHP versions simultaneously
<?php function truncateGumbo($string, $chars = 50, $terminator = ' …') { $cutPos = $chars - mb_strlen($terminator); $boundaryPos = mb_strrpos(mb_substr($string, 0, mb_strpos($string, ' ', $cutPos)), ' '); return mb_substr($string, 0, $boundaryPos === false ? $cutPos : $boundaryPos) . $terminator; } function truncateGordon($string, $chars = 50, $terminator = ' …') { return mb_strimwidth($string, 0, $chars, $terminator); } function truncateSoapBox($string, $chars = 50, $terminate = ' …') { $chars -= mb_strlen($terminate); if ( $chars <= 0 ) return $terminate; $string = mb_substr($string, 0, $chars); $space = mb_strrpos($string, ' '); if ($space < mb_strlen($string) / 2) return $string . $terminate; else return mb_substr($string, 0, $space) . $terminate; } function truncateMickmackusa($string, $max = 50, $terminator = ' …') { $trunc = $max - mb_strlen($terminator, 'UTF-8'); return preg_replace("~(?=.{{$max}})(?:\S{{$trunc}}|.{0,$trunc}(?=\s))\K.+~us", $terminator, $string); } $tests = [ [ 'testCase' => "Answer to the Ultimate Question of Life, the Universe, and Everything.", // 50th char ---------------------------------------------------^ 'expected' => "Answer to the Ultimate Question of Life, the …", ], [ 'testCase' => "A single line of text to be followed by another\nline of text", // 50th char ----------------------------------------------------^ 'expected' => "A single line of text to be followed by another …", ], [ 'testCase' => "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝ", // 50th char ---------------------------------------------------^ 'expected' => "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđ …", ], [ 'testCase' => "123456789 123456789 123456789 123456789 123456789", // 50th char doesn't exist -------------------------------------^ 'expected' => "1234567890123456789012345678901234567890123456789", ], [ 'testCase' => "Hello worldly world", // 50th char doesn't exist -------------------------------------^ 'expected' => "Hello worldly world", ], [ 'testCase' => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890", // 50th char ---------------------------------------------------^ 'expected' => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV …", ], ]; foreach ($tests as ['testCase' => $testCase, 'expected' => $expected]) { echo "\tSample Input:\t\t$testCase\n"; echo "\n\ttruncateGumbo:\t\t" , truncateGumbo($testCase); echo "\n\ttruncateGordon:\t\t" , truncateGordon($testCase); echo "\n\ttruncateSoapBox:\t" , truncateSoapBox($testCase); echo "\n\ttruncateMickmackusa:\t" , truncateMickmackusa($testCase); echo "\n\tExpected Result:\t{$expected}"; echo "\n-----------------------------------------------------\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 37
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 37
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename:       /in/ijGiR
function name:  (null)
number of ops:  39
compiled vars:  !0 = $tests, !1 = $testCase, !2 = $expected
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   ASSIGN                                                   !0, <array>
   66     1      > FE_RESET_R                                       $4      !0, ->37
          2    > > FE_FETCH_R                                               $4, $5, ->37
          3    >   FETCH_LIST_R                                     $6      $5, 'testCase'
          4        ASSIGN                                                   !1, $6
          5        FETCH_LIST_R                                     $8      $5, 'expected'
          6        ASSIGN                                                   !2, $8
          7        FREE                                                     $5
   67     8        ROPE_INIT                                     3  ~11     '%09Sample+Input%3A%09%09'
          9        ROPE_ADD                                      1  ~11     ~11, !1
         10        ROPE_END                                      2  ~10     ~11, '%0A'
         11        ECHO                                                     ~10
   68    12        ECHO                                                     '%0A%09truncateGumbo%3A%09%09'
         13        INIT_FCALL                                               'truncategumbo'
         14        SEND_VAR                                                 !1
         15        DO_FCALL                                      0  $13     
         16        ECHO                                                     $13
   69    17        ECHO                                                     '%0A%09truncateGordon%3A%09%09'
         18        INIT_FCALL                                               'truncategordon'
         19        SEND_VAR                                                 !1
         20        DO_FCALL                                      0  $14     
         21        ECHO                                                     $14
   70    22        ECHO                                                     '%0A%09truncateSoapBox%3A%09'
         23        INIT_FCALL                                               'truncatesoapbox'
         24        SEND_VAR                                                 !1
         25        DO_FCALL                                      0  $15     
         26        ECHO                                                     $15
   71    27        ECHO                                                     '%0A%09truncateMickmackusa%3A%09'
         28        INIT_FCALL                                               'truncatemickmackusa'
         29        SEND_VAR                                                 !1
         30        DO_FCALL                                      0  $16     
         31        ECHO                                                     $16
   72    32        NOP                                                      
         33        FAST_CONCAT                                      ~17     '%0A%09Expected+Result%3A%09', !2
         34        ECHO                                                     ~17
   73    35        ECHO                                                     '%0A-----------------------------------------------------%0A'
   66    36      > JMP                                                      ->2
         37    >   FE_FREE                                                  $4
   74    38      > RETURN                                                   1

Function truncategumbo:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ijGiR
function name:  truncateGumbo
number of ops:  36
compiled vars:  !0 = $string, !1 = $chars, !2 = $terminator, !3 = $cutPos, !4 = $boundaryPos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      50
          2        RECV_INIT                                        !2      '+%E2%80%A6'
    3     3        INIT_FCALL                                               'mb_strlen'
          4        SEND_VAR                                                 !2
          5        DO_ICALL                                         $5      
          6        SUB                                              ~6      !1, $5
          7        ASSIGN                                                   !3, ~6
    4     8        INIT_FCALL                                               'mb_strrpos'
          9        INIT_FCALL                                               'mb_substr'
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 0
         12        INIT_FCALL                                               'mb_strpos'
         13        SEND_VAR                                                 !0
         14        SEND_VAL                                                 '+'
         15        SEND_VAR                                                 !3
         16        DO_ICALL                                         $8      
         17        SEND_VAR                                                 $8
         18        DO_ICALL                                         $9      
         19        SEND_VAR                                                 $9
         20        SEND_VAL                                                 '+'
         21        DO_ICALL                                         $10     
         22        ASSIGN                                                   !4, $10
    5    23        INIT_FCALL                                               'mb_substr'
         24        SEND_VAR                                                 !0
         25        SEND_VAL                                                 0
         26        TYPE_CHECK                                    4          !4
         27      > JMPZ                                                     ~12, ->30
         28    >   QM_ASSIGN                                        ~13     !3
         29      > JMP                                                      ->31
         30    >   QM_ASSIGN                                        ~13     !4
         31    >   SEND_VAL                                                 ~13
         32        DO_ICALL                                         $14     
         33        CONCAT                                           ~15     $14, !2
         34      > RETURN                                                   ~15
    6    35*     > RETURN                                                   null

End of function truncategumbo

Function truncategordon:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ijGiR
function name:  truncateGordon
number of ops:  11
compiled vars:  !0 = $string, !1 = $chars, !2 = $terminator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      50
          2        RECV_INIT                                        !2      '+%E2%80%A6'
    9     3        INIT_FCALL                                               'mb_strimwidth'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 0
          6        SEND_VAR                                                 !1
          7        SEND_VAR                                                 !2
          8        DO_ICALL                                         $3      
          9      > RETURN                                                   $3
   10    10*     > RETURN                                                   null

End of function truncategordon

Function truncatesoapbox:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 30
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ijGiR
function name:  truncateSoapBox
number of ops:  38
compiled vars:  !0 = $string, !1 = $chars, !2 = $terminate, !3 = $space
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      50
          2        RECV_INIT                                        !2      '+%E2%80%A6'
   15     3        INIT_FCALL                                               'mb_strlen'
          4        SEND_VAR                                                 !2
          5        DO_ICALL                                         $4      
          6        ASSIGN_OP                                     2          !1, $4
   16     7        IS_SMALLER_OR_EQUAL                                      !1, 0
          8      > JMPZ                                                     ~6, ->10
   17     9    > > RETURN                                                   !2
   19    10    >   INIT_FCALL                                               'mb_substr'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 0
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                         $7      
         15        ASSIGN                                                   !0, $7
   20    16        INIT_FCALL                                               'mb_strrpos'
         17        SEND_VAR                                                 !0
         18        SEND_VAL                                                 '+'
         19        DO_ICALL                                         $9      
         20        ASSIGN                                                   !3, $9
   22    21        INIT_FCALL                                               'mb_strlen'
         22        SEND_VAR                                                 !0
         23        DO_ICALL                                         $11     
         24        DIV                                              ~12     $11, 2
         25        IS_SMALLER                                               !3, ~12
         26      > JMPZ                                                     ~13, ->30
   23    27    >   CONCAT                                           ~14     !0, !2
         28      > RETURN                                                   ~14
   22    29*       JMP                                                      ->37
   25    30    >   INIT_FCALL                                               'mb_substr'
         31        SEND_VAR                                                 !0
         32        SEND_VAL                                                 0
         33        SEND_VAR                                                 !3
         34        DO_ICALL                                         $15     
         35        CONCAT                                           ~16     $15, !2
         36      > RETURN                                                   ~16
   26    37*     > RETURN                                                   null

End of function truncatesoapbox

Function truncatemickmackusa:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ijGiR
function name:  truncateMickmackusa
number of ops:  23
compiled vars:  !0 = $string, !1 = $max, !2 = $terminator, !3 = $trunc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      50
          2        RECV_INIT                                        !2      '+%E2%80%A6'
   29     3        INIT_FCALL                                               'mb_strlen'
          4        SEND_VAR                                                 !2
          5        SEND_VAL                                                 'UTF-8'
          6        DO_ICALL                                         $4      
          7        SUB                                              ~5      !1, $4
          8        ASSIGN                                                   !3, ~5
   30     9        INIT_FCALL                                               'preg_replace'
         10        ROPE_INIT                                     7  ~8      '%7E%28%3F%3D.%7B'
         11        ROPE_ADD                                      1  ~8      ~8, !1
         12        ROPE_ADD                                      2  ~8      ~8, '%7D%29%28%3F%3A%5CS%7B'
         13        ROPE_ADD                                      3  ~8      ~8, !3
         14        ROPE_ADD                                      4  ~8      ~8, '%7D%7C.%7B0%2C'
         15        ROPE_ADD                                      5  ~8      ~8, !3
         16        ROPE_END                                      6  ~7      ~8, '%7D%28%3F%3D%5Cs%29%29%5CK.%2B%7Eus'
         17        SEND_VAL                                                 ~7
         18        SEND_VAR                                                 !2
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                         $12     
         21      > RETURN                                                   $12
   31    22*     > RETURN                                                   null

End of function truncatemickmackusa

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
243.8 ms | 1019 KiB | 23 Q