3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Html{ protected $reachedLimit = false, $totalLen = 0, $maxLen = 25; public static function trim($html, $maxLen = 25){ $dom = new DomDocument(); $dom->loadHTML($html); $html = new static(); $html->walk($dom, $maxLen); // remove wrapper tags added by DD (doctype, html...) // http://stackoverflow.com/a/6953808/1058140 $dom->removeChild($dom->firstChild); $dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild); return $dom->saveHTML(); } protected function walk(DomNode $node, $maxLen){ // remove any nodes that passed our limit if($this->reachedLimit){ $node->parentNode->removeChild($node); return; } // only text nodes should have text, // so do the splitting here if($node instanceof DomText){ $this->totalLen += $nodeLen = strlen($node->nodeValue); if($this->totalLen > $maxLen){ $node->nodeValue = substr($node->nodeValue, 0, $nodeLen - ($this->totalLen - $maxLen)); $this->reachedLimit = true; } } if(!$node->hasChildNodes()) return; // if node has children, walk its child elements foreach($node->childNodes as $child) $this->walk($child, $maxLen); } } 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/4Mma6
function name:  (null)
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     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
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4Mma6
function name:  trim
number of ops:  34
compiled vars:  !0 = $html, !1 = $maxLen, !2 = $dom
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      25
   14     2        NEW                                              $3      'DomDocument'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !2, $3
   15     5        INIT_METHOD_CALL                                         !2, 'loadHTML'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
   18     8        NEW                          static              $7      
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !0, $7
   19    11        INIT_METHOD_CALL                                         !0, 'walk'
         12        SEND_VAR_EX                                              !2
         13        SEND_VAR_EX                                              !1
         14        DO_FCALL                                      0          
   23    15        INIT_METHOD_CALL                                         !2, 'removeChild'
         16        CHECK_FUNC_ARG                                           
         17        FETCH_OBJ_FUNC_ARG                               $11     !2, 'firstChild'
         18        SEND_FUNC_ARG                                            $11
         19        DO_FCALL                                      0          
   24    20        INIT_METHOD_CALL                                         !2, 'replaceChild'
         21        CHECK_FUNC_ARG                                           
         22        FETCH_OBJ_FUNC_ARG                               $13     !2, 'firstChild'
         23        FETCH_OBJ_FUNC_ARG                               $14     $13, 'firstChild'
         24        FETCH_OBJ_FUNC_ARG                               $15     $14, 'firstChild'
         25        SEND_FUNC_ARG                                            $15
         26        CHECK_FUNC_ARG                                           
         27        FETCH_OBJ_FUNC_ARG                               $16     !2, 'firstChild'
         28        SEND_FUNC_ARG                                            $16
         29        DO_FCALL                                      0          
   26    30        INIT_METHOD_CALL                                         !2, 'saveHTML'
         31        DO_FCALL                                      0  $18     
         32      > RETURN                                                   $18
   27    33*     > 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 = 9
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 32
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 32
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 37
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
2 jumps found. (Code = 77) Position 1 = 39, Position 2 = 45
Branch analysis from position: 39
2 jumps found. (Code = 78) Position 1 = 40, Position 2 = 45
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
Branch analysis from position: 45
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
Branch analysis from position: 32
Branch analysis from position: 32
filename:       /in/4Mma6
function name:  walk
number of ops:  47
compiled vars:  !0 = $node, !1 = $maxLen, !2 = $nodeLen, !3 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   32     2        FETCH_OBJ_R                                      ~4      'reachedLimit'
          3      > JMPZ                                                     ~4, ->9
   33     4    >   FETCH_OBJ_R                                      ~5      !0, 'parentNode'
          5        INIT_METHOD_CALL                                         ~5, 'removeChild'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
   34     8      > RETURN                                                   null
   39     9    >   INSTANCEOF                                               !0, 'DomText'
         10      > JMPZ                                                     ~7, ->32
   40    11    >   FETCH_OBJ_R                                      ~9      !0, 'nodeValue'
         12        STRLEN                                           ~10     ~9
         13        ASSIGN                                           ~11     !2, ~10
         14        ASSIGN_OBJ_OP                                 1          'totalLen'
         15        OP_DATA                                                  ~11
   42    16        FETCH_OBJ_R                                      ~12     'totalLen'
         17        IS_SMALLER                                               !1, ~12
         18      > JMPZ                                                     ~13, ->32
   43    19    >   INIT_FCALL                                               'substr'
         20        FETCH_OBJ_R                                      ~15     !0, 'nodeValue'
         21        SEND_VAL                                                 ~15
         22        SEND_VAL                                                 0
         23        FETCH_OBJ_R                                      ~16     'totalLen'
         24        SUB                                              ~17     ~16, !1
         25        SUB                                              ~18     !2, ~17
         26        SEND_VAL                                                 ~18
         27        DO_ICALL                                         $19     
         28        ASSIGN_OBJ                                               !0, 'nodeValue'
         29        OP_DATA                                                  $19
   44    30        ASSIGN_OBJ                                               'reachedLimit'
         31        OP_DATA                                                  <true>
   49    32    >   INIT_METHOD_CALL                                         !0, 'hasChildNodes'
         33        DO_FCALL                                      0  $21     
         34        BOOL_NOT                                         ~22     $21
         35      > JMPZ                                                     ~22, ->37
   50    36    > > RETURN                                                   null
   53    37    >   FETCH_OBJ_R                                      ~23     !0, 'childNodes'
         38      > FE_RESET_R                                       $24     ~23, ->45
         39    > > FE_FETCH_R                                               $24, !3, ->45
   54    40    >   INIT_METHOD_CALL                                         'walk'
         41        SEND_VAR_EX                                              !3
         42        SEND_VAR_EX                                              !1
         43        DO_FCALL                                      0          
   53    44      > JMP                                                      ->39
         45    >   FE_FREE                                                  $24
   55    46      > RETURN                                                   null

End of function walk

End of class Html.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.38 ms | 1404 KiB | 15 Q