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 id="mct_ai_excerpt">The <a class="zem_slink" title="Class action" href="http://en.wikipedia.org/wiki/Class_action" target="_blank" rel="wikipedia">class action</a> Complaint was filed by the San Francisco labor law lawyers at Blumenthal, Nordrehaug &amp; Bhowmik. The lawsuit, filed on July 17, 2014, alleges that Kmart Corporation misclassified their Loss Prevention Managers as exempt from overtime pay and as a result, failed to pay overtime wages, and failed to provide meal and rest breaks, as required by the California Labor Code. The lawsuit entitled, Moua, et al. v. Kmart Corporation &amp; Sears Holdings Management Corporation, Case No. 14CECG02033 is currently pending in the Fresno County Superior Court for the State of California. The Kmart Corporation complaint alleges that the Plaintiff, who worked as a Loss Prevention Manager, engaged in non-exempt tasks throughout his workday, including ensuring the proper operation and repairs of alarm equipment, detaining and processing shoplifters, and observing and reviewing store <a class="zem_slink" title="Security Camera Videos" href="http://www.break.com/topics/security-camera-videos" target="_blank" rel="break">surveillance cameras</a> among other things. The Complaint further claims that the Plaintiff and other Loss Prevention Managers are allegedly "managers in name only" as they do not direct the work of other employees and spend most of their day engaging in non-exempt job tasks. As such, the Complaint claims these employees have been misclassified under California law and seeks an unspecified amount of back overtime pay for the Plaintiff and the proposed class. The firm also represents a class of Loss Prevention Managers who worked for Frys Electronics claiming alleged unpaid overtime wages, Steffan v. Frys Electronics, Inc. That case is pending in the <a class="zem_slink" title="Santa Clara County Superior Court" href="http://www.sccsuperiorcourt.org/" target="_blank" rel="homepage">Santa Clara County Superior Court</a>, Case No. 1-13-CV-2541011.</p>', 150);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RBOJI
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+id%3D%22mct_ai_excerpt%22%3EThe+%3Ca+class%3D%22zem_slink%22+title%3D%22Class+action%22+href%3D%22http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FClass_action%22+target%3D%22_blank%22+rel%3D%22wikipedia%22%3Eclass+action%3C%2Fa%3E+Complaint+was+filed+by+the+San+Francisco+labor+law+lawyers+at+Blumenthal%2C+Nordrehaug+%26amp%3B+Bhowmik.+The+lawsuit%2C+filed+on+July+17%2C+2014%2C+alleges+that+Kmart+Corporation+misclassified+their+Loss+Prevention+Managers+as+exempt+from+overtime+pay+and+as+a+result%2C+failed+to+pay+overtime+wages%2C+and+failed+to+provide+meal+and+rest+breaks%2C+as+required+by+the+California+Labor+Code.+The+lawsuit+entitled%2C+Moua%2C+et+al.+v.+Kmart+Corporation+%26amp%3B+Sears+Holdings+Management+Corporation%2C+Case+No.+14CECG02033+is+currently+pending+in+the+Fresno+County+Superior+Court+for+the+State+of+California.+The+Kmart+Corporation+complaint+alleges+that+the+Plaintiff%2C+who+worked+as+a+Loss+Prevention+Manager%2C+engaged+in+non-exempt+tasks+throughout+his+workday%2C+including+ensuring+the+proper+operation+and+repairs+of+alarm+equipment%2C+detaining+and+processing+shoplifters%2C+and+observing+and+reviewing+store+%3Ca+class%3D%22zem_slink%22+title%3D%22Security+Camera+Videos%22+href%3D%22http%3A%2F%2Fwww.break.com%2Ftopics%2Fsecurity-camera-videos%22+target%3D%22_blank%22+rel%3D%22break%22%3Esurveillance+cameras%3C%2Fa%3E+among+other+things.+The+Complaint+further+claims+that+the+Plaintiff+and+other+Loss+Prevention+Managers+are+allegedly+%22managers+in+name+only%22+as+they+do+not+direct+the+work+of+other+employees+and+spend+most+of+their+day+engaging+in+non-exempt+job+tasks.+As+such%2C+the+Complaint+claims+these+employees+have+been+misclassified+under+California+law+and+seeks+an+unspecified+amount+of+back+overtime+pay+for+the+Plaintiff+and+the+proposed+class.+The+firm+also+represents+a+class+of+Loss+Prevention+Managers+who+worked+for+Frys+Electronics+claiming+alleged+unpaid+overtime+wages%2C+Steffan+v.+Frys+Electronics%2C+Inc.+That+case+is+pending+in+the+%3Ca+class%3D%22zem_slink%22+title%3D%22Santa+Clara+County+Superior+Court%22+href%3D%22http%3A%2F%2Fwww.sccsuperiorcourt.org%2F%22+target%3D%22_blank%22+rel%3D%22homepage%22%3ESanta+Clara+County+Superior+Court%3C%2Fa%3E%2C+Case+No.+1-13-CV-2541011.%3C%2Fp%3E'
          2        SEND_VAL                                                 150
          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/RBOJI
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/RBOJI
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:
167.39 ms | 1400 KiB | 17 Q