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(); if (version_compare(PHP_VERSION, '5.4.0') < 0) { $dom->loadHTML($html); } else { $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); } $instance = new static(); $toRemove = $instance->walk($dom, $maxLen); // remove any nodes that exceed limit foreach ($toRemove as $child) { $child->parentNode->removeChild($child); } // remove wrapper tags added by DD (doctype, html...) if (version_compare(PHP_VERSION, '5.4.0') < 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(); } 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>Lorem ipsum dolor sit amet <a href="very-long-long-prelong-href-that-should-not-be-cut">Quite long link</a> text after link</p>', 35);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sGcfG
function name:  (null)
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   INIT_STATIC_METHOD_CALL                                  'Html', 'trim'
          1        SEND_VAL                                                 '%3Cp%3ELorem+ipsum+dolor+sit+amet+%3Ca+href%3D%22very-long-long-prelong-href-that-should-not-be-cut%22%3EQuite+long+link%3C%2Fa%3E+text+after+link%3C%2Fp%3E'
          2        SEND_VAL                                                 35
          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 = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 77) Position 1 = 28, Position 2 = 34
Branch analysis from position: 28
2 jumps found. (Code = 78) Position 1 = 29, Position 2 = 34
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 59
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 59
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
Branch analysis from position: 15
2 jumps found. (Code = 77) Position 1 = 28, Position 2 = 34
Branch analysis from position: 28
Branch analysis from position: 34
filename:       /in/sGcfG
function name:  trim
number of ops:  63
compiled vars:  !0 = $html, !1 = $maxLen, !2 = $dom, !3 = $instance, !4 = $toRemove, !5 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      25
   14     2        NEW                                              $6      'DomDocument'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !2, $6
   16     5        INIT_FCALL                                               'version_compare'
          6        SEND_VAL                                                 '8.0.0'
          7        SEND_VAL                                                 '5.4.0'
          8        DO_ICALL                                         $9      
          9        IS_SMALLER                                               $9, 0
         10      > JMPZ                                                     ~10, ->15
   17    11    >   INIT_METHOD_CALL                                         !2, 'loadHTML'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0          
         14      > JMP                                                      ->19
   19    15    >   INIT_METHOD_CALL                                         !2, 'loadHTML'
         16        SEND_VAR_EX                                              !0
         17        SEND_VAL_EX                                              8196
         18        DO_FCALL                                      0          
   22    19    >   NEW                          static              $13     
         20        DO_FCALL                                      0          
         21        ASSIGN                                                   !3, $13
   23    22        INIT_METHOD_CALL                                         !3, 'walk'
         23        SEND_VAR_EX                                              !2
         24        SEND_VAR_EX                                              !1
         25        DO_FCALL                                      0  $16     
         26        ASSIGN                                                   !4, $16
   26    27      > FE_RESET_R                                       $18     !4, ->34
         28    > > FE_FETCH_R                                               $18, !5, ->34
   27    29    >   FETCH_OBJ_R                                      ~19     !5, 'parentNode'
         30        INIT_METHOD_CALL                                         ~19, 'removeChild'
         31        SEND_VAR_EX                                              !5
         32        DO_FCALL                                      0          
   26    33      > JMP                                                      ->28
         34    >   FE_FREE                                                  $18
   31    35        INIT_FCALL                                               'version_compare'
         36        SEND_VAL                                                 '8.0.0'
         37        SEND_VAL                                                 '5.4.0'
         38        DO_ICALL                                         $21     
         39        IS_SMALLER                                               $21, 0
         40      > JMPZ                                                     ~22, ->59
   33    41    >   INIT_METHOD_CALL                                         !2, 'removeChild'
         42        CHECK_FUNC_ARG                                           
         43        FETCH_OBJ_FUNC_ARG                               $23     !2, 'firstChild'
         44        SEND_FUNC_ARG                                            $23
         45        DO_FCALL                                      0          
   34    46        INIT_METHOD_CALL                                         !2, 'replaceChild'
         47        CHECK_FUNC_ARG                                           
         48        FETCH_OBJ_FUNC_ARG                               $25     !2, 'firstChild'
         49        FETCH_OBJ_FUNC_ARG                               $26     $25, 'firstChild'
         50        FETCH_OBJ_FUNC_ARG                               $27     $26, 'firstChild'
         51        SEND_FUNC_ARG                                            $27
         52        CHECK_FUNC_ARG                                           
         53        FETCH_OBJ_FUNC_ARG                               $28     !2, 'firstChild'
         54        SEND_FUNC_ARG                                            $28
         55        DO_FCALL                                      0          
   36    56        INIT_METHOD_CALL                                         !2, 'saveHTML'
         57        DO_FCALL                                      0  $30     
         58      > RETURN                                                   $30
   39    59    >   INIT_METHOD_CALL                                         !2, 'saveHTML'
         60        DO_FCALL                                      0  $31     
         61      > RETURN                                                   $31
   40    62*     > 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/sGcfG
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
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   45     2        FETCH_OBJ_R                                      ~4      'reachedLimit'
          3      > JMPZ                                                     ~4, ->8
   46     4    >   FETCH_OBJ_W                                      $5      'toRemove'
          5        ASSIGN_DIM                                               $5
          6        OP_DATA                                                  !0
          7      > JMP                                                      ->43
   50     8    >   INSTANCEOF                                               !0, 'DomText'
          9      > JMPZ                                                     ~7, ->32
   51    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
   54    15        FETCH_OBJ_R                                      ~12     'totalLen'
         16        IS_SMALLER                                               !1, ~12
         17      > JMPZ                                                     ~13, ->32
   55    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
   56    30        ASSIGN_OBJ                                               'reachedLimit'
         31        OP_DATA                                                  <true>
   61    32    >   ISSET_ISEMPTY_PROP_OBJ                                   !0, 'childNodes'
         33      > JMPZ                                                     ~22, ->43
   62    34    >   FETCH_OBJ_R                                      ~23     !0, 'childNodes'
         35      > FE_RESET_R                                       $24     ~23, ->42
         36    > > FE_FETCH_R                                               $24, !3, ->42
   63    37    >   INIT_METHOD_CALL                                         'walk'
         38        SEND_VAR_EX                                              !3
         39        SEND_VAR_EX                                              !1
         40        DO_FCALL                                      0          
   62    41      > JMP                                                      ->36
         42    >   FE_FREE                                                  $24
   68    43    >   FETCH_OBJ_R                                      ~26     'toRemove'
         44      > RETURN                                                   ~26
   69    45*     > RETURN                                                   null

End of function walk

End of class Html.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
174.14 ms | 956 KiB | 18 Q