3v4l.org

run code in 500+ PHP versions simultaneously
<?php class Html { public static function load($html) { $document = <<<EOD <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> <body>!html</body> </html> EOD; // PHP's \DOMDocument serialization adds extra whitespace when the markup // of the wrapping document contains newlines, so ensure we remove all // newlines before injecting the actual HTML body to be processed. $document = strtr($document, ["\n" => '', '!html' => $html]); $dom = new \DOMDocument(); // Ignore warnings during HTML soup loading. @$dom->loadHTML($document); return $dom; } /** * Converts the body of a \DOMDocument back to an HTML snippet. * * The function serializes the body part of a \DOMDocument back to an (X)HTML * snippet. The resulting (X)HTML snippet will be properly formatted to be * compatible with HTML user agents. * * @param \DOMDocument $document * A \DOMDocument object to serialize, only the tags below the first <body> * node will be converted. * * @return string * A valid (X)HTML snippet, as a string. */ public static function serialize(\DOMDocument $document) { $body_node = $document->getElementsByTagName('body')->item(0); $html = ''; if ($body_node !== NULL) { foreach ($body_node->childNodes as $node) { $html .= $document->saveXML($node); } } return $html; } } $source = <<<EOT <table> <tr> <td>L</td> <td>R</td> </tr> </table> EOT; $document = Html::load($source); $out['document'] = "\n" . $document->saveHTML(); $body = $document->getElementsByTagName('body')->item(0); $out['body'] = "\n" . $document->saveXML($body); $table = $body->childNodes[0]; $out['table'] = "\n" . $document->saveXML($table); $out['serialize'] = "\n" . Html::serialize($document); var_export($out);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2jSIa
function name:  (null)
number of ops:  42
compiled vars:  !0 = $source, !1 = $document, !2 = $out, !3 = $body, !4 = $table
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   53     0  E >   ASSIGN                                                       !0, '%3Ctable%3E%0A++%3Ctr%3E%0A++++%3Ctd%3EL%3C%2Ftd%3E%0A++++%3Ctd%3ER%3C%2Ftd%3E%0A++%3C%2Ftr%3E%0A%3C%2Ftable%3E'
   62     1        INIT_STATIC_METHOD_CALL                                      'Html', 'load'
          2        SEND_VAR                                                     !0
          3        DO_FCALL                                          0  $6      
          4        ASSIGN                                                       !1, $6
   63     5        INIT_METHOD_CALL                                             !1, 'saveHTML'
          6        DO_FCALL                                          0  $9      
          7        CONCAT                                               ~10     '%0A', $9
          8        ASSIGN_DIM                                                   !2, 'document'
          9        OP_DATA                                                      ~10
   65    10        INIT_METHOD_CALL                                             !1, 'getElementsByTagName'
         11        SEND_VAL_EX                                                  'body'
         12        DO_FCALL                                          0  $11     
         13        INIT_METHOD_CALL                                             $11, 'item'
         14        SEND_VAL_EX                                                  0
         15        DO_FCALL                                          0  $12     
         16        ASSIGN                                                       !3, $12
   66    17        INIT_METHOD_CALL                                             !1, 'saveXML'
         18        SEND_VAR_EX                                                  !3
         19        DO_FCALL                                          0  $15     
         20        CONCAT                                               ~16     '%0A', $15
         21        ASSIGN_DIM                                                   !2, 'body'
         22        OP_DATA                                                      ~16
   68    23        FETCH_OBJ_R                                          ~17     !3, 'childNodes'
         24        FETCH_DIM_R                                          ~18     ~17, 0
         25        ASSIGN                                                       !4, ~18
   69    26        INIT_METHOD_CALL                                             !1, 'saveXML'
         27        SEND_VAR_EX                                                  !4
         28        DO_FCALL                                          0  $21     
         29        CONCAT                                               ~22     '%0A', $21
         30        ASSIGN_DIM                                                   !2, 'table'
         31        OP_DATA                                                      ~22
   71    32        INIT_STATIC_METHOD_CALL                                      'Html', 'serialize'
         33        SEND_VAR                                                     !1
         34        DO_FCALL                                          0  $24     
         35        CONCAT                                               ~25     '%0A', $24
         36        ASSIGN_DIM                                                   !2, 'serialize'
         37        OP_DATA                                                      ~25
   73    38        INIT_FCALL                                                   'var_export'
         39        SEND_VAR                                                     !2
         40        DO_ICALL                                                     
         41      > RETURN                                                       1

Class Html:
Function load:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2jSIa
function name:  load
number of ops:  16
compiled vars:  !0 = $html, !1 = $document, !2 = $dom
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    5     0  E >   RECV                                                 !0      
    6     1        ASSIGN                                                       !1, '%3C%21DOCTYPE+html+PUBLIC+%22-%2F%2FW3C%2F%2FDTD+XHTML+1.0+Strict%2F%2FEN%22+%22http%3A%2F%2Fwww.w3.org%2FTR%2Fxhtml1%2FDTD%2Fxhtml1-strict.dtd%22%3E%0A%3Chtml+xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxhtml%22%3E%0A%3Chead%3E%3Cmeta+http-equiv%3D%22Content-Type%22+content%3D%22text%2Fhtml%3B+charset%3Dutf-8%22+%2F%3E%3C%2Fhead%3E%0A%3Cbody%3E%21html%3C%2Fbody%3E%0A%3C%2Fhtml%3E'
   16     2        INIT_ARRAY                                           ~4      '', '%0A'
          3        ADD_ARRAY_ELEMENT                                    ~4      !0, '%21html'
          4        FRAMELESS_ICALL_2                strtr               ~5      !1, ~4
          5        ASSIGN                                                       !1, ~5
   18     6        NEW                                                  $7      'DOMDocument'
          7        DO_FCALL                                          0          
          8        ASSIGN                                                       !2, $7
   20     9        BEGIN_SILENCE                                        ~10     
         10        INIT_METHOD_CALL                                             !2, 'loadHTML'
         11        SEND_VAR_EX                                                  !1
         12        DO_FCALL                                          0          
         13        END_SILENCE                                                  ~10
   22    14      > RETURN                                                       !2
   23    15*     > RETURN                                                       null

End of function load

Function serialize:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 20
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 19
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 19
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
Branch analysis from position: 20
filename:       /in/2jSIa
function name:  serialize
number of ops:  22
compiled vars:  !0 = $document, !1 = $body_node, !2 = $html, !3 = $node
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   39     0  E >   RECV                                                 !0      
   40     1        INIT_METHOD_CALL                                             !0, 'getElementsByTagName'
          2        SEND_VAL_EX                                                  'body'
          3        DO_FCALL                                          0  $4      
          4        INIT_METHOD_CALL                                             $4, 'item'
          5        SEND_VAL_EX                                                  0
          6        DO_FCALL                                          0  $5      
          7        ASSIGN                                                       !1, $5
   41     8        ASSIGN                                                       !2, ''
   43     9        TYPE_CHECK                                      1020          !1
         10      > JMPZ                                                         ~8, ->20
   44    11    >   FETCH_OBJ_R                                          ~9      !1, 'childNodes'
         12      > FE_RESET_R                                           $10     ~9, ->19
         13    > > FE_FETCH_R                                                   $10, !3, ->19
   45    14    >   INIT_METHOD_CALL                                             !0, 'saveXML'
         15        SEND_VAR_EX                                                  !3
         16        DO_FCALL                                          0  $11     
         17        ASSIGN_OP                                         8          !2, $11
   44    18      > JMP                                                          ->13
         19    >   FE_FREE                                                      $10
   48    20    > > RETURN                                                       !2
   49    21*     > RETURN                                                       null

End of function serialize

End of class Html.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
170.8 ms | 2115 KiB | 14 Q