3v4l.org

run code in 500+ 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 trim </strong>', 14);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rvpMU
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+trim+%3C%2Fstrong%3E'
          2        SEND_VAL                                                     14
          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/rvpMU
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.5.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 = 40
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 29
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 29
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 40
Branch analysis from position: 31
2 jumps found. (Code = 77) Position 1 = 33, Position 2 = 39
Branch analysis from position: 33
2 jumps found. (Code = 78) Position 1 = 34, Position 2 = 39
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
Branch analysis from position: 40
Branch analysis from position: 29
Branch analysis from position: 29
filename:       /in/rvpMU
function name:  walk
number of ops:  43
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
   38     7      > JMP                                                          ->40
   44     8    >   INSTANCEOF                                                   !0, 'DomText'
          9      > JMPZ                                                         ~7, ->29
   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, ->29
   49    18    >   FETCH_OBJ_R                                          ~15     !0, 'nodeValue'
         19        FETCH_OBJ_R                                          ~16     'totalLen'
         20        SUB                                                  ~17     ~16, !1
         21        SUB                                                  ~18     !2, ~17
         22        FRAMELESS_ICALL_3                substr              ~19     ~15, 0
         23        OP_DATA                                                      ~18
         24        CONCAT                                               ~20     ~19, '...'
         25        ASSIGN_OBJ                                                   !0, 'nodeValue'
         26        OP_DATA                                                      ~20
   50    27        ASSIGN_OBJ                                                   'reachedLimit'
         28        OP_DATA                                                      <true>
   56    29    >   ISSET_ISEMPTY_PROP_OBJ                                       !0, 'childNodes'
         30      > JMPZ                                                         ~22, ->40
   57    31    >   FETCH_OBJ_R                                          ~23     !0, 'childNodes'
         32      > FE_RESET_R                                           $24     ~23, ->39
         33    > > FE_FETCH_R                                                   $24, !3, ->39
   58    34    >   INIT_METHOD_CALL                                             'walk'
         35        SEND_VAR_EX                                                  !3
         36        SEND_VAR_EX                                                  !1
         37        DO_FCALL                                          0          
   57    38      > JMP                                                          ->33
         39    >   FE_FREE                                                      $24
   61    40    >   FETCH_OBJ_R                                          ~26     'toRemove'
         41      > RETURN                                                       ~26
   62    42*     > RETURN                                                       null

End of function walk

End of class Html.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
161 ms | 3026 KiB | 14 Q