3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Html{ protected $reachedLimit = false, $totalLen = 0, $maxLen = 25, $toRemove = array(); public static function trim($html, $maxLen = 25){ $dom = new DomDocument(); $dom->loadHTML($html); $html = new static(); $toRemove = $html->walk($dom, $maxLen); // remove any nodes that passed our limit foreach($toRemove as $child) $child->parentNode->removeChild($child); // remove wrapper tags added by DD (doctype, html...) if(version_compare(PHP_VERSION, '5.3.6') < 0){ // http://stackoverflow.com/a/6953808/1058140 $dom->removeChild($dom->firstChild); $dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild); return $dom->saveHTML(); } return $dom->saveHTML($dom->getElementsByTagName('body')->item(0)); } protected function walk(DomNode $node, $maxLen){ if($this->reachedLimit){ $this->toRemove[] = $node; }else{ // only text nodes should have text, // so do the splitting here if($node instanceof DomText){ $this->totalLen += $nodeLen = strlen($node->nodeValue); // use mb_strlen / mb_substr for UTF-8 support if($this->totalLen > $maxLen){ $node->nodeValue = substr($node->nodeValue, 0, $nodeLen - ($this->totalLen - $maxLen)) . ''; $this->reachedLimit = true; } } // if node has children, walk its child elements if(isset($node->childNodes)) foreach($node->childNodes as $child) $this->walk($child, $maxLen); } return $this->toRemove; } } print html::trim('<p>I can haz <strong> html sadsdsdsd trim </strong>', 15);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Jt5ap
function name:  (null)
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   INIT_STATIC_METHOD_CALL                                  'html', 'trim'
          1        SEND_VAL                                                 '%3Cp%3EI+can+haz+%3Cstrong%3E+html+sadsdsdsd+trim+%3C%2Fstrong%3E'
          2        SEND_VAL                                                 15
          3        DO_FCALL                                      0  $0      
          4        ECHO                                                     $0
          5      > RETURN                                                   1

Class Html:
Function trim:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 23
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 23
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 48
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/Jt5ap
function name:  trim
number of ops:  59
compiled vars:  !0 = $html, !1 = $maxLen, !2 = $dom, !3 = $toRemove, !4 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      25
   15     2        NEW                                              $5      'DomDocument'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !2, $5
   16     5        INIT_METHOD_CALL                                         !2, 'loadHTML'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
   18     8        NEW                          static              $9      
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !0, $9
   19    11        INIT_METHOD_CALL                                         !0, 'walk'
         12        SEND_VAR_EX                                              !2
         13        SEND_VAR_EX                                              !1
         14        DO_FCALL                                      0  $12     
         15        ASSIGN                                                   !3, $12
   22    16      > FE_RESET_R                                       $14     !3, ->23
         17    > > FE_FETCH_R                                               $14, !4, ->23
   23    18    >   FETCH_OBJ_R                                      ~15     !4, 'parentNode'
         19        INIT_METHOD_CALL                                         ~15, 'removeChild'
         20        SEND_VAR_EX                                              !4
         21        DO_FCALL                                      0          
   22    22      > JMP                                                      ->17
         23    >   FE_FREE                                                  $14
   26    24        INIT_FCALL                                               'version_compare'
         25        SEND_VAL                                                 '8.0.0'
         26        SEND_VAL                                                 '5.3.6'
         27        DO_ICALL                                         $17     
         28        IS_SMALLER                                               $17, 0
         29      > JMPZ                                                     ~18, ->48
   28    30    >   INIT_METHOD_CALL                                         !2, 'removeChild'
         31        CHECK_FUNC_ARG                                           
         32        FETCH_OBJ_FUNC_ARG                               $19     !2, 'firstChild'
         33        SEND_FUNC_ARG                                            $19
         34        DO_FCALL                                      0          
   29    35        INIT_METHOD_CALL                                         !2, 'replaceChild'
         36        CHECK_FUNC_ARG                                           
         37        FETCH_OBJ_FUNC_ARG                               $21     !2, 'firstChild'
         38        FETCH_OBJ_FUNC_ARG                               $22     $21, 'firstChild'
         39        FETCH_OBJ_FUNC_ARG                               $23     $22, 'firstChild'
         40        SEND_FUNC_ARG                                            $23
         41        CHECK_FUNC_ARG                                           
         42        FETCH_OBJ_FUNC_ARG                               $24     !2, 'firstChild'
         43        SEND_FUNC_ARG                                            $24
         44        DO_FCALL                                      0          
   30    45        INIT_METHOD_CALL                                         !2, 'saveHTML'
         46        DO_FCALL                                      0  $26     
         47      > RETURN                                                   $26
   33    48    >   INIT_METHOD_CALL                                         !2, 'saveHTML'
         49        INIT_METHOD_CALL                                         !2, 'getElementsByTagName'
         50        SEND_VAL_EX                                              'body'
         51        DO_FCALL                                      0  $27     
         52        INIT_METHOD_CALL                                         $27, 'item'
         53        SEND_VAL_EX                                              0
         54        DO_FCALL                                      0  $28     
         55        SEND_VAR_NO_REF_EX                                       $28
         56        DO_FCALL                                      0  $29     
         57      > RETURN                                                   $29
   34    58*     > RETURN                                                   null

End of function trim

Function walk:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 32
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 32
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 43
Branch analysis from position: 34
2 jumps found. (Code = 77) Position 1 = 36, Position 2 = 42
Branch analysis from position: 36
2 jumps found. (Code = 78) Position 1 = 37, Position 2 = 42
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
Branch analysis from position: 43
Branch analysis from position: 32
Branch analysis from position: 32
filename:       /in/Jt5ap
function name:  walk
number of ops:  46
compiled vars:  !0 = $node, !1 = $maxLen, !2 = $nodeLen, !3 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   38     2        FETCH_OBJ_R                                      ~4      'reachedLimit'
          3      > JMPZ                                                     ~4, ->8
   39     4    >   FETCH_OBJ_W                                      $5      'toRemove'
          5        ASSIGN_DIM                                               $5
          6        OP_DATA                                                  !0
          7      > JMP                                                      ->43
   44     8    >   INSTANCEOF                                               !0, 'DomText'
          9      > JMPZ                                                     ~7, ->32
   45    10    >   FETCH_OBJ_R                                      ~9      !0, 'nodeValue'
         11        STRLEN                                           ~10     ~9
         12        ASSIGN                                           ~11     !2, ~10
         13        ASSIGN_OBJ_OP                                 1          'totalLen'
         14        OP_DATA                                                  ~11
   48    15        FETCH_OBJ_R                                      ~12     'totalLen'
         16        IS_SMALLER                                               !1, ~12
         17      > JMPZ                                                     ~13, ->32
   49    18    >   INIT_FCALL                                               'substr'
         19        FETCH_OBJ_R                                      ~15     !0, 'nodeValue'
         20        SEND_VAL                                                 ~15
         21        SEND_VAL                                                 0
         22        FETCH_OBJ_R                                      ~16     'totalLen'
         23        SUB                                              ~17     ~16, !1
         24        SUB                                              ~18     !2, ~17
         25        SEND_VAL                                                 ~18
         26        DO_ICALL                                         $19     
         27        CONCAT                                           ~20     $19, ''
         28        ASSIGN_OBJ                                               !0, 'nodeValue'
         29        OP_DATA                                                  ~20
   50    30        ASSIGN_OBJ                                               'reachedLimit'
         31        OP_DATA                                                  <true>
   56    32    >   ISSET_ISEMPTY_PROP_OBJ                                   !0, 'childNodes'
         33      > JMPZ                                                     ~22, ->43
   57    34    >   FETCH_OBJ_R                                      ~23     !0, 'childNodes'
         35      > FE_RESET_R                                       $24     ~23, ->42
         36    > > FE_FETCH_R                                               $24, !3, ->42
   58    37    >   INIT_METHOD_CALL                                         'walk'
         38        SEND_VAR_EX                                              !3
         39        SEND_VAR_EX                                              !1
         40        DO_FCALL                                      0          
   57    41      > JMP                                                      ->36
         42    >   FE_FREE                                                  $24
   61    43    >   FETCH_OBJ_R                                      ~26     'toRemove'
         44      > RETURN                                                   ~26
   62    45*     > RETURN                                                   null

End of function walk

End of class Html.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.32 ms | 1396 KiB | 17 Q