3v4l.org

run code in 300+ PHP versions simultaneously
<?php function printTruncated($maxLength, $html, $isUtf8=true) { $printedLength = 0; $position = 0; $tags = array(); // For UTF-8, we need to count multibyte sequences as one character. $re = $isUtf8 ? '{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;|[\x80-\xFF][\x80-\xBF]*}' : '{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}'; while ($printedLength < $maxLength && preg_match($re, $html, $match, PREG_OFFSET_CAPTURE, $position)) { list($tag, $tagPosition) = $match[0]; // Print text leading up to the tag. $str = substr($html, $position, $tagPosition - $position); if ($printedLength + strlen($str) > $maxLength) { print(substr($str, 0, $maxLength - $printedLength)); $printedLength = $maxLength; break; } print($str); $printedLength += strlen($str); if ($printedLength >= $maxLength) break; if ($tag[0] == '&' || ord($tag) >= 0x80) { // Pass the entity or UTF-8 multibyte sequence through unchanged. print($tag); $printedLength++; } else { // Handle the tag. $tagName = $match[1][0]; if ($tag[1] == '/') { // This is a closing tag. $openingTag = array_pop($tags); assert($openingTag == $tagName); // check that tags are properly nested. print($tag); } else if ($tag[strlen($tag) - 2] == '/') { // Self-closing tag. print($tag); } else { // Opening tag. print($tag); $tags[] = $tagName; } } // Continue after the tag. $position = $tagPosition + strlen($tag); } // Print any remaining text. if ($printedLength < $maxLength && $position < strlen($html)) print(substr($html, $position, $maxLength - $printedLength)); // Close any open tags. while (!empty($tags)) printf('</%s>', array_pop($tags)); } printTruncated(10, '<b>&lt;Hello&gt;</b> <img src="world.png" alt="" /> world!'); print("\n"); printTruncated(10, '<table><tr><td>Heck, </td><td>throw</td></tr><tr><td>in a</td><td>table</td></tr></table>'); print("\n"); printTruncated(10, "<em><b>Hello</b>&#20;w\xC3\xB8rld!</em>"); print("\n"); printTruncated(500, "<div>AAAAAAAAA</div> <div><ol><li>xxxxxxxxxxxxxxx</li><li>yyyyyyyy<b>yyyyyyyyy</b>yyyyyyyy</li><li>zzzzzzz</li></ol>ccccccccccccccccccccccccccccccccccc<br> cccccccccccccccccccccccccccccccccccccccc<br> cccccccccccccccccccccccccccccccccccccccc<br> cccccccccccccccccccccccccccccccccccccccc </div>"); print("\n");
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlQjQ
function name:  (null)
number of ops:  21
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   INIT_FCALL                                               'printtruncated'
          1        SEND_VAL                                                 10
          2        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'
          3        DO_FCALL                                      0          
          4        ECHO                                                     '%0A'
   79     5        INIT_FCALL                                               'printtruncated'
          6        SEND_VAL                                                 10
          7        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'
          8        DO_FCALL                                      0          
          9        ECHO                                                     '%0A'
   81    10        INIT_FCALL                                               'printtruncated'
         11        SEND_VAL                                                 10
         12        SEND_VAL                                                 '%3Cem%3E%3Cb%3EHello%3C%2Fb%3E%26%2320%3Bw%C3%B8rld%21%3C%2Fem%3E'
         13        DO_FCALL                                      0          
         14        ECHO                                                     '%0A'
   83    15        INIT_FCALL                                               'printtruncated'
         16        SEND_VAL                                                 500
         17        SEND_VAL                                                 '%3Cdiv%3EAAAAAAAAA%3C%2Fdiv%3E%0A%3Cdiv%3E%3Col%3E%3Cli%3Exxxxxxxxxxxxxxx%3C%2Fli%3E%3Cli%3Eyyyyyyyy%3Cb%3Eyyyyyyyyy%3C%2Fb%3Eyyyyyyyy%3C%2Fli%3E%3Cli%3Ezzzzzzz%3C%2Fli%3E%3C%2Fol%3Eccccccccccccccccccccccccccccccccccc%3Cbr%3E%0Acccccccccccccccccccccccccccccccccccccccc%3Cbr%3E%0Acccccccccccccccccccccccccccccccccccccccc%3Cbr%3E%0Acccccccccccccccccccccccccccccccccccccccc%0A%3C%2Fdiv%3E'
         18        DO_FCALL                                      0          
   88    19        ECHO                                                     '%0A'
         20      > RETURN                                                   1

Function printtruncated:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 87
Branch analysis from position: 87
2 jumps found. (Code = 46) Position 1 = 89, Position 2 = 97
Branch analysis from position: 89
2 jumps found. (Code = 44) Position 1 = 98, Position 2 = 12
Branch analysis from position: 98
2 jumps found. (Code = 46) Position 1 = 100, Position 2 = 103
Branch analysis from position: 100
2 jumps found. (Code = 43) Position 1 = 104, Position 2 = 111
Branch analysis from position: 104
1 jumps found. (Code = 42) Position 1 = 119
Branch analysis from position: 119
2 jumps found. (Code = 44) Position 1 = 122, Position 2 = 112
Branch analysis from position: 122
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 112
2 jumps found. (Code = 44) Position 1 = 122, Position 2 = 112
Branch analysis from position: 122
Branch analysis from position: 112
Branch analysis from position: 111
Branch analysis from position: 103
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 38
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 98
Branch analysis from position: 98
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 44
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 98
Branch analysis from position: 98
Branch analysis from position: 44
2 jumps found. (Code = 47) Position 1 = 47, Position 2 = 52
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 56
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 84
Branch analysis from position: 84
2 jumps found. (Code = 46) Position 1 = 89, Position 2 = 97
Branch analysis from position: 89
Branch analysis from position: 97
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 74
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 84
Branch analysis from position: 84
Branch analysis from position: 74
2 jumps found. (Code = 43) Position 1 = 79, Position 2 = 81
Branch analysis from position: 79
1 jumps found. (Code = 42) Position 1 = 84
Branch analysis from position: 84
Branch analysis from position: 81
2 jumps found. (Code = 46) Position 1 = 89, Position 2 = 97
Branch analysis from position: 89
Branch analysis from position: 97
Branch analysis from position: 52
Branch analysis from position: 97
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 87
Branch analysis from position: 87
filename:       /in/qlQjQ
function name:  printTruncated
number of ops:  123
compiled vars:  !0 = $maxLength, !1 = $html, !2 = $isUtf8, !3 = $printedLength, !4 = $position, !5 = $tags, !6 = $re, !7 = $match, !8 = $tag, !9 = $tagPosition, !10 = $str, !11 = $tagName, !12 = $openingTag
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <true>
    5     3        ASSIGN                                                   !3, 0
    6     4        ASSIGN                                                   !4, 0
    7     5        ASSIGN                                                   !5, <array>
   10     6      > JMPZ                                                     !2, ->9
   11     7    >   QM_ASSIGN                                        ~16     '%7B%3C%2F%3F%28%5Ba-z%5D%2B%29%5B%5E%3E%5D%2A%3E%7C%26%23%3F%5Ba-zA-Z0-9%5D%2B%3B%7C%5B%5Cx80-%5CxFF%5D%5B%5Cx80-%5CxBF%5D%2A%7D'
          8      > JMP                                                      ->10
   12     9    >   QM_ASSIGN                                        ~16     '%7B%3C%2F%3F%28%5Ba-z%5D%2B%29%5B%5E%3E%5D%2A%3E%7C%26%23%3F%5Ba-zA-Z0-9%5D%2B%3B%7D'
   10    10    >   ASSIGN                                                   !6, ~16
   14    11      > JMP                                                      ->87
   16    12    >   FETCH_DIM_R                                      ~18     !7, 0
         13        FETCH_LIST_R                                     $19     ~18, 0
         14        ASSIGN                                                   !8, $19
         15        FETCH_LIST_R                                     $21     ~18, 1
         16        ASSIGN                                                   !9, $21
         17        FREE                                                     ~18
   19    18        INIT_FCALL                                               'substr'
         19        SEND_VAR                                                 !1
         20        SEND_VAR                                                 !4
         21        SUB                                              ~23     !9, !4
         22        SEND_VAL                                                 ~23
         23        DO_ICALL                                         $24     
         24        ASSIGN                                                   !10, $24
   20    25        STRLEN                                           ~26     !10
         26        ADD                                              ~27     !3, ~26
         27        IS_SMALLER                                               !0, ~27
         28      > JMPZ                                                     ~28, ->38
   22    29    >   INIT_FCALL                                               'substr'
         30        SEND_VAR                                                 !10
         31        SEND_VAL                                                 0
         32        SUB                                              ~29     !0, !3
         33        SEND_VAL                                                 ~29
         34        DO_ICALL                                         $30     
         35        ECHO                                                     $30
   23    36        ASSIGN                                                   !3, !0
   24    37      > JMP                                                      ->98
   27    38    >   ECHO                                                     !10
   28    39        STRLEN                                           ~32     !10
         40        ASSIGN_OP                                     1          !3, ~32
   29    41        IS_SMALLER_OR_EQUAL                                      !0, !3
         42      > JMPZ                                                     ~34, ->44
         43    > > JMP                                                      ->98
   31    44    >   FETCH_DIM_R                                      ~35     !8, 0
         45        IS_EQUAL                                         ~36     ~35, '%26'
         46      > JMPNZ_EX                                         ~36     ~36, ->52
         47    >   INIT_FCALL                                               'ord'
         48        SEND_VAR                                                 !8
         49        DO_ICALL                                         $37     
         50        IS_SMALLER_OR_EQUAL                              ~38     128, $37
         51        BOOL                                             ~36     ~38
         52    > > JMPZ                                                     ~36, ->56
   34    53    >   ECHO                                                     !8
   35    54        PRE_INC                                                  !3
         55      > JMP                                                      ->84
   40    56    >   FETCH_DIM_R                                      ~40     !7, 1
         57        FETCH_DIM_R                                      ~41     ~40, 0
         58        ASSIGN                                                   !11, ~41
   41    59        FETCH_DIM_R                                      ~43     !8, 1
         60        IS_EQUAL                                                 ~43, '%2F'
         61      > JMPZ                                                     ~44, ->74
   45    62    >   INIT_FCALL                                               'array_pop'
         63        SEND_REF                                                 !5
         64        DO_ICALL                                         $45     
         65        ASSIGN                                                   !12, $45
   46    66        ASSERT_CHECK                                             
         67        INIT_FCALL                                               'assert'
         68        IS_EQUAL                                         ~47     !12, !11
         69        SEND_VAL                                                 ~47
         70        SEND_VAL                                                 'assert%28%24openingTag+%3D%3D+%24tagName%29'
         71        DO_ICALL                                                 
   48    72        ECHO                                                     !8
         73      > JMP                                                      ->84
   50    74    >   STRLEN                                           ~49     !8
         75        SUB                                              ~50     ~49, 2
         76        FETCH_DIM_R                                      ~51     !8, ~50
         77        IS_EQUAL                                                 ~51, '%2F'
         78      > JMPZ                                                     ~52, ->81
   53    79    >   ECHO                                                     !8
         80      > JMP                                                      ->84
   58    81    >   ECHO                                                     !8
   59    82        ASSIGN_DIM                                               !5
         83        OP_DATA                                                  !11
   64    84    >   STRLEN                                           ~54     !8
         85        ADD                                              ~55     !9, ~54
         86        ASSIGN                                                   !4, ~55
   14    87    >   IS_SMALLER                                       ~57     !3, !0
         88      > JMPZ_EX                                          ~57     ~57, ->97
         89    >   INIT_FCALL                                               'preg_match'
         90        SEND_VAR                                                 !6
         91        SEND_VAR                                                 !1
         92        SEND_REF                                                 !7
         93        SEND_VAL                                                 256
         94        SEND_VAR                                                 !4
         95        DO_ICALL                                         $58     
         96        BOOL                                             ~57     $58
         97    > > JMPNZ                                                    ~57, ->12
   68    98    >   IS_SMALLER                                       ~59     !3, !0
         99      > JMPZ_EX                                          ~59     ~59, ->103
        100    >   STRLEN                                           ~60     !1
        101        IS_SMALLER                                       ~61     !4, ~60
        102        BOOL                                             ~59     ~61
        103    > > JMPZ                                                     ~59, ->111
   69   104    >   INIT_FCALL                                               'substr'
        105        SEND_VAR                                                 !1
        106        SEND_VAR                                                 !4
        107        SUB                                              ~62     !0, !3
        108        SEND_VAL                                                 ~62
        109        DO_ICALL                                         $63     
        110        ECHO                                                     $63
   72   111    > > JMP                                                      ->119
   73   112    >   INIT_FCALL                                               'printf'
        113        SEND_VAL                                                 '%3C%2F%25s%3E'
        114        INIT_FCALL                                               'array_pop'
        115        SEND_REF                                                 !5
        116        DO_ICALL                                         $64     
        117        SEND_VAR                                                 $64
        118        DO_ICALL                                                 
   72   119    >   ISSET_ISEMPTY_CV                                 ~66     !5
        120        BOOL_NOT                                         ~67     ~66
        121      > JMPNZ                                                    ~67, ->112
   74   122    > > RETURN                                                   null

End of function printtruncated

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
167.84 ms | 1411 KiB | 29 Q